Using a native non-COM dll

  • Thread starter Thread starter Hadi
  • Start date Start date
H

Hadi

Can C# uses a native non-COM dll? I tried to add reference to an non-COM dll
and I can't.

thanks,

Hadi
 
Hi Hadi,

When using native dlls, you don't need to reference the dll file
explicitly. All you have to do is use .NET's PInvoke services:


Here's an example:

using System.Runtime.InteropServices;

class PlatformInvokeTest
{
[DllImport("user32.dll")]
/* You have to write the dll method's signature in c# as 'extern'*/

public static extern int MessageBoxA(
int h, string m, string c, int type);

public static int Main()
{
return MessageBoxA(0, "Hello World!", "My Message Box", 0);
}
}

If you want further informationm there's a few examples in MSDN


HTH,

Ramiro
 
Hadi, yes, check out the dllimport attribute. You can import almost any
dll.
 
What if my dll is not Windows dll? Where should I put my dll to invoke? If I
just say DllImport("dll.dll") how would c# know where the location of my
dll?


thanks

Ramiro Calderón said:
Hi Hadi,

When using native dlls, you don't need to reference the dll file
explicitly. All you have to do is use .NET's PInvoke services:


Here's an example:

using System.Runtime.InteropServices;

class PlatformInvokeTest
{
[DllImport("user32.dll")]
/* You have to write the dll method's signature in c# as 'extern'*/

public static extern int MessageBoxA(
int h, string m, string c, int type);

public static int Main()
{
return MessageBoxA(0, "Hello World!", "My Message Box", 0);
}
}

If you want further informationm there's a few examples in MSDN


HTH,

Ramiro
Can C# uses a native non-COM dll? I tried to add reference to an non-COM dll
and I can't.

thanks,

Hadi
 
Where should I put my dll to invoke? If I
just say DllImport("dll.dll") how would c# know where the location of my
dll?

You should be able to put it in the application directory.



Mattias
 
Hadi, you can also specify the complete path to your DLL. This can be
problematic if you are distributing your application to machines which don't
have the dll in the same place. I would go with Mattias's suggestion unless
it's absolutely necessary to put the path.

--
Greg Ewing [MVP]
http://www.citidc.com

Hadi said:
What if my dll is not Windows dll? Where should I put my dll to invoke? If I
just say DllImport("dll.dll") how would c# know where the location of my
dll?


thanks

Ramiro Calderón said:
Hi Hadi,

When using native dlls, you don't need to reference the dll file
explicitly. All you have to do is use .NET's PInvoke services:


Here's an example:

using System.Runtime.InteropServices;

class PlatformInvokeTest
{
[DllImport("user32.dll")]
/* You have to write the dll method's signature in c# as 'extern'*/

public static extern int MessageBoxA(
int h, string m, string c, int type);

public static int Main()
{
return MessageBoxA(0, "Hello World!", "My Message Box", 0);
}
}

If you want further informationm there's a few examples in MSDN


HTH,

Ramiro
Can C# uses a native non-COM dll? I tried to add reference to an
non-COM
 
Back
Top