Adding an unmanaged DLL

B

ba.hons

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
 
E

Egghead

You have the ans already :)

You do not need to add ref to your project. It is not a COM object. It is
only a win32 dll.
Do the dllimport is good and "using System.Runtime.InteropServices;" , just
make sure the dll is in the windows\system32 or in the bin folder (at run
time)

cheers,
RL
 
W

Willy Denoyette [MVP]

ba.hons said:
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



There is really one good answer to this question, ask the vendor.
If the DLL is built to be consumable by arbitrary code, he should supply you
the necessary documentation that explains how you can call the DLL
functions, if he's not willing or not able to supply this documentation,
stay away from it, never use undocumented stuff.

Willy.
 
B

ba.hons

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!

Adam

There is really one good answer to this question, ask the vendor.
If the DLL is built to be consumable by arbitrary code, he should supply you
the necessary documentation that explains how you can call the DLL
functions, if he's not willing or not able to supply this documentation,
stay away from it, never use undocumented stuff.

Willy.


A perfect point i did manage to get through to them today and was told
that is was not supported. I now have problems with using the PINVOKE
stuff but at least i know how i need to approach this.

Thanks everyone for the comments!

Adam
 
W

Willy Denoyette [MVP]

ba.hons said:
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!

Adam

There is really one good answer to this question, ask the vendor.
If the DLL is built to be consumable by arbitrary code, he should supply
you
the necessary documentation that explains how you can call the DLL
functions, if he's not willing or not able to supply this documentation,
stay away from it, never use undocumented stuff.

Willy.


A perfect point i did manage to get through to them today and was told
that is was not supported. I now have problems with using the PINVOKE
stuff but at least i know how i need to approach this.

Thanks everyone for the comments!

Adam


If not supported, you should stay away from it, really. You will never be
able to use this DLL without wasting a lot of time trying to call function
by trial and error.
Why don't you reimplement the API's in managed code?


Willy.
 

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