C# and Win32 DLLs

P

peter.amberg

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
 
K

Kerem Gümrükcü

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/library/d91k01sh(VS.80).aspx

[Module-Definition (.def) Files]
http://msdn.microsoft.com/en-us/library/28d6s79h(VS.80).aspx

[Exporting from a DLL]
http://msdn.microsoft.com/en-us/library/z4zxe9k8(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

--
 
P

pagerintas pritupimas

to disable name mangling delcare your c++ function like this:

extern "C" __declspec(dllexport) int __cdecl TestAdd (int a, int b)
 
P

Pavel Minaev

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top