473,395 Members | 1,936 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

C# and Win32 DLLs

Hi

I'm trying to write a C++ DLL and use it in C#. In C++, I created a
Win32 project and added a function

__declspec(dllexport) int __cdecl TestAdd (int a, int b)
{
return a + b;
}

I gather I do not need an Export.def file when using
__declspec(dllexport), right? I could compile the DLL, anyhow.

In C#, I write:

[DllImport(@"C:\bla\bla\mydll.dll", EntryPoint="TestAdd")]
public static extern int TestAdd(int a, int b);

This compiles, but I get an EntryPointNotFound exception at runtime. I
guess my TestAdd method was not really exported. Is there a quick way
to view all exported symbols of a DLL? Any other ideas?

Thanks,
Peter
Sep 15 '08 #1
3 1655
Hi Peter,

you want a dll with a c-exported functions, thats what you want.
You need a DEF File to export the functions, read this for C/C++
side:

[Exporting from a DLL Using DEF Files]
http://msdn.microsoft.com/en-us/libr...sh(VS.80).aspx

[Module-Definition (.def) Files]
http://msdn.microsoft.com/en-us/libr...9h(VS.80).aspx

[Exporting from a DLL]
http://msdn.microsoft.com/en-us/libr...k8(VS.80).aspx

Read this for C# .NET side:

[Calling Win32 DLLs in C# with P/Invoke]
http://msdn.microsoft.com/en-us/magazine/cc164123.aspx

Its an good introduction if you are new to this topics,...
Always take care of memory boundaries and right
cleanup routines in C/C++ code, respective try/catch/finally
and stuff like memory allocation/deallocation. This is all
up to your C/C++ code and the .NET runtime has no
responsibility on this!

Hope this helps,...
Regards

Kerem

--
-----------------------
Beste Grüsse / Best regards / Votre bien devoue
Kerem Gümrükcü
Latest Project: http://www.codeplex.com/restarts
Latest Open-Source Projects: http://entwicklung.junetz.de
-----------------------
"This reply is provided as is, without warranty express or implied."
<pe**********@gmail.comschrieb im Newsbeitrag
news:62**********************************@f36g2000 hsa.googlegroups.com...
Hi

I'm trying to write a C++ DLL and use it in C#. In C++, I created a
Win32 project and added a function

__declspec(dllexport) int __cdecl TestAdd (int a, int b)
{
return a + b;
}

I gather I do not need an Export.def file when using
__declspec(dllexport), right? I could compile the DLL, anyhow.

In C#, I write:

[DllImport(@"C:\bla\bla\mydll.dll", EntryPoint="TestAdd")]
public static extern int TestAdd(int a, int b);

This compiles, but I get an EntryPointNotFound exception at runtime. I
guess my TestAdd method was not really exported. Is there a quick way
to view all exported symbols of a DLL? Any other ideas?

Thanks,
Peter
Sep 15 '08 #2
to disable name mangling delcare your c++ function like this:

extern "C" __declspec(dllexport) int __cdecl TestAdd (int a, int b)
Sep 15 '08 #3
On Sep 15, 1:49*pm, peter.amb...@gmail.com wrote:
Hi

I'm trying to write a C++ DLL and use it in C#. In C++, I created a
Win32 project and added a function

__declspec(dllexport) int __cdecl TestAdd (int a, int b)
* *{
* *return a + b;
* *}

I gather I do not need an Export.def file when using
__declspec(dllexport), right? I could compile the DLL, anyhow.

In C#, I write:

* * * * [DllImport(@"C:\bla\bla\mydll.dll", EntryPoint="TestAdd")]
* * * * public static extern int TestAdd(int a, int b);

This compiles, but I get an EntryPointNotFound exception at runtime. I
guess my TestAdd method was not really exported. Is there a quick way
to view all exported symbols of a DLL? Any other ideas?
The default calling convention that C# assumes is stdcall, not cdecl.
This has an impact on name mangling of exported functions (cdecl
prefixes them with underscore, stdcall doesn't), which is why it
couldn't find your function. So, just declare your C++ functions as
__stdcall, not __cdecl.

Oh, and you do not need a .def file if you use __declspec(dllexport),
despite what Kerem says.
Sep 18 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jorgen Grahn | last post by:
I couldn't think of a good solution, and it's hard to Google for... I write python command-line programs under Win2k, and I use the bash shell from Cygwin. I cannot use Cygwin's python package...
6
by: Laszlo Zsolt Nagy | last post by:
Sorry, I realized that the import zlib was not executed from my (working) service. So here is the question: why can't I use zlib from a win32 service? Is there any way to make it working? ...
2
by: ramialhasan | last post by:
I have good experience with COM ATL object, but recently I needed to deal with Win32 DLLs. I have some question relating to their model of work. In COM objects every client of the com server...
6
by: _R | last post by:
I've had to write a lot of code to interface C# to older Win32 DLLs. Basically, an unmanaged C++ class talks directly to the Win32 DLL. A managed C++ class encloses the unmanaged C++ class. C#...
24
by: Dave | last post by:
I understand that VS.NET is supposed to compile native Win32 apps that do not require the .Net runtime. If that's the case then there is something else from the VS200x package that is required. ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.