Unmanaged code

  • Thread starter Thread starter Jason
  • Start date Start date
J

Jason

Hello

I've got a DLL from a 3rd party vendor and they wrote the DLL in C++ so I
can't just easily add a reference to it. How do I add that dll, to my C#
application?

Any idea, examples?
 
Hello Jason,

Using platform invoke
Read there http://msdn2.microsoft.com/en-gb/library/ms173184.aspx and see
sample http://msdn2.microsoft.com/en-gb/library/ms173187.aspx of calling
WinAPI


J> Hello
J>
J> I've got a DLL from a 3rd party vendor and they wrote the DLL in C++
J> so I can't just easily add a reference to it. How do I add that dll,
J> to my C# application?
J>
J> Any idea, examples?
J>
---
WBR,
Michael Nemtsev :: blog: http://spaces.live.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
Ok

I see the........

System.Runtime.InteropServices.DllImport("winmm.DLL", EntryPoint =
"PlaySound", SetLastError = true)]
private static extern bool PlaySound(string szSound, System.IntPtr
hMod, PlaySoundFlags flags);


but how do know or find out what the entry point of a 3rd party DLL would
be?
 
Hello Jason,

Obviously from the your dll manuals. They should provide u the list of functions
they export
btw, read a bit there http://samples.gotdotnet.com/quickstart/howto/doc/Interop/PInvoke_Simple.aspx

J> Ok
J>
J> I see the........
J>
J> System.Runtime.InteropServices.DllImport("winmm.DLL", EntryPoint =
J> "PlaySound", SetLastError = true)]
J> private static extern bool PlaySound(string szSound,
J> System.IntPtr
J> hMod, PlaySoundFlags flags);
J> but how do know or find out what the entry point of a 3rd party DLL
J> would be?
J>
J> J>---
WBR,
Michael Nemtsev :: blog: http://spaces.live.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
Jason said:
Ok

I see the........

System.Runtime.InteropServices.DllImport("winmm.DLL", EntryPoint = "PlaySound",
SetLastError = true)]
private static extern bool PlaySound(string szSound, System.IntPtr hMod,
PlaySoundFlags flags);


but how do know or find out what the entry point of a 3rd party DLL would be?

Are you sure the DLL is exporting C style functions, that is, is the DLL meant to be used
from clients using "C style" bindings? C# and PInvoke cannot call exported C++ class
members. Another thing you need to know is the calling convention used for the exports. You
can try to get at the exports using tools like depends.exe and dumpbin.exe from the platform
SDK, but what you really need is the documentation that describes the exported functions
their argument types ,calling convention and semantics .

Willy.
 
Hi Jason,

You only need the EntryPoint if you choose a different name. For example
this is a part of code to access a DLL of me:

[DllImport(Tools.dllName, EntryPoint = "Initialize")]
public static extern void InitializeApi();
[DllImport(Tools.dllName)]
public static extern void Disconnect();

The first one, 'Initialize' is an error in C# because it is already
existant, so I renamed it to InitializeApi. The second one 'Disconnect'
I did not rename, so no EntryPoint needed.

hope this helps.
 

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

Back
Top