Can't get PInvoke to work

J

John Dann

I'm trying unsuccessfully to access some functions within an external
DLL; sorry I don't know much about DLL classification, but AFAIK it's
not an ActiveX DLL, but a vanilla one written probably in C.

I've copied the DLL to my project folder (mydll.dll), but beyond that
haven't added any reference to it because I can't see how and don't
know if I need to anyhow.

My class code consists of (just one function as an example):

Imports System.Runtime.InteropServices
Public Class VPWLIO
<DllImport("mydll")> _
Public Shared Function OpenCommPort_V(ByVal comport As Int16,
ByVal baudrate As Integer) As Int16

End Function
End Class

(Sorry the word wrap has broken the lines a little)

I can instantiate this class OK in the calling module and Intellisense
shows up the method so that part's OK. But on running the project I
get an error that the DLL can't be loaded.

This is the first time I've tried to do this and so I'm probably
making some fundamental mistake, but can anyone shed any further light
please?

NB The function name inside the DLL is documented to be exactly how
I've spelled it in the function definition.

TIA
JGD
 
J

John Dann

I can instantiate this class OK in the calling module and Intellisense
shows up the method so that part's OK. But on running the project I
get an error that the DLL can't be loaded.

Sorry could have been a little more explicit - the actual error is a
DLLNotFoundException. So presumably my project can't see the DLL even
though it's in the project folder - perhaps there _is_ something more
I should be doing to make the DLL visible.

NB The project is saved on to a remote network folder, but this folder
has been given FullTrust status and saves the projects OK. But is
there some reason why it might not load a DLL from such a location?

JGD
 
J

John Dann

OK, problem solved - I think. For development purposes the DLL seems
to need to be in the \bin subfolder of the project folder - perhaps
not surprising on reflection. The test function now appears to work
OK.

JGD
 
A

Anders Sundberg

Hi,

isn't also a good idea to check and if not, register the dll in the system
within your app startup sequence. That way you can put wherever you like
As I recall it the *.exe looks for the *.dll in its 'own' folder, the
windows\system32 folder and if it has been registered?.

For testing I put the *.dll in the *exe folder, and when shipping I make
sure that the *.dll is registered with the installation app.

/anders
 

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