utilizing unmanaged C++ library

  • Thread starter Thread starter SteveK
  • Start date Start date
S

SteveK

Hi-

I know this is common topic, but I don't intend to ask you guys "how" but
rather "Where can I find samples or tutorials that actually compile??" ;)

I have been attempting to educate myself on this topic for awhile and 3
attempts to download source code and compile have yielded nothing. My most
recent problem is "System.DllNotFound" exception. The DLL is located in the
same directory as my .cs code. I tried to add a reference but recieved an
error that it wasn't a valid type.

I would just really like to see a working example of using an unmanaged code
in C#. Anyone know a good place to read more?

Thanks-
Steve
 
Thanks for the quick answer, Ming,

I have added my call to DllImport and when I build the project, VS.NET
crashes SUPER fast, if I comment out the DllImport line, it's fine.
Isn't that nice? ;(


-Steve
 
Hi Steve,

What I had as a problem once is name mangling. Be sure to export your
entries from the C++ library as follows.

extern "C"
{
/* your exports */
}

Other than that, you should have no problem using the DllImportAttribute. A
small sample that works for me :

[ DllImport( "my_lib.dll", EntryPoint = "my_exported_function" ) ]
public static extern int MyExportedFunction (
[ MarshalAs( UnmanagedType.LPStr ) ] string fileName,
[ MarshalAs( UnmanagedType.U4 ) ] uint timeoutMs
);

// ...
int i = MyExportedFunction( "file.xml", 10000 );

If you still have problems using the dll, you could post a code-fragment
illustrating the problem...

HTH,
Tom T.
 
Back
Top