Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding an unmanaged DLL

Status
Not open for further replies.

adamsmithhrs

Programmer
Apr 25, 2007
1
GB
Hello,

wondering if somone could clear something up for me as i have been reading around this subject for a while and am still confused.

I have a DLL from a hardware vendor but i dont know what language the DLL is written in.

I would like to use this DLL from managed code (c#). From what i have read i need this to be a COM object, but i dont really understand how you differentiate between a dll written in C++ which is a COM object and on that isnt.

Each time i try to add the dll as a reference (VS 2003 .Net 2.0) i get the following message:

A reference to MYDLL.dll could not be added. This is not a valid assembly or COM component.

I have tried using PINVOKE see code below and managed to call a method from the DLL using my c# application but i dont really want to write a method for each method the DLL supports, mainly becuase i am worried about casting data types etc

[DllImport("MyDLL.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern bool AlarmLogSize(int lpDirectoryName,
out uint lpFreeBytesAvailable);

Form1.AlarmLogSize(0, out freeBytesAvail);
MessageBox.Show(freeBytesAvail.ToString());

Can anyone tell me how best i can use this DLL and what my options are.

Also i have an example application written in VB6 which come with the DLL and that has a Module which seems looks like this:

Public Declare Function AlarmLogSize Lib "VertX.dll" (ByVal mode As Long, _
logSize As Long) As Long

I have no idea when it comes to VB but i presume this is the equivilent of what am doing with my PINVOKE applicaiton.

Any help really appreicated!

Thanks

Adam
 
The error message and the sample from the sdk simply shows your DLL is a win32 dll, not a COM. So with plain C#, you have to use pinvoke to import the functions.

And you're presumption is correct. That VB code is the equivalent of your C# extern method.

[wink]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top