String to IntPtr conversion

  • Thread starter Thread starter massimo capetola
  • Start date Start date
M

massimo capetola

I use vb.net 2003 to developp a program for PocketPc 2003.
I have a problem to pass parameters into a function of a C++ dll

Here is my function's declaration in vb.net:
Public Declare Function InitDll lib "Mydll" (ByVal initFile as IntPtr,
ByVal pnumber as long) as long

initFile is a path of a file : for example "\toto\inifile.txt"

How can I convert my string path in a IntPtr type?
 
That doesn't seem right, the "long" types are a red flag, and the IntPtr for
the string indicates maybe a pointer to a pointer? Can you post the method
signature from the DLL?

It's probably something like this:
LONG InitDll(TCHAR *intiFile, DWORD pnumber);

which would probably be something like this:
Public Declare Function InitDll lib "Mydll" (ByRef initFile as String, ByVal
pnumber as Integer) as Integer
 
int = Integer, not Long
char * = Byte(), _not_ a String in CE (it's ANSI, not Unicode)
 
Thanks it's OK for this function!!!

but I have a another function to call in my project:

C++ declaration :
char * GetDllVersion(void);

My vb.net declaration
Public Declare Function GetDllVersion lib "Mydll" () as byte()

and when I call my function like this:

Dim lVersion as byte()

lVersion = GetDllVersion()

I have an "NotSupportedException" error. What I made wrong?
 
Well... in this particular case you need to use IntPtr and a return type and
then to use Marshal.Copy to copy bytes to the managed array.
 
I've never thought returning a string from a function was a good idea,
largely becasue of both length and deallocation problems. Where is the
string declared you pass back? Who deallocates it? How long is it?

I would suggest this:
BOOL GetDllVersion(TCHAR *version, DWORD *length);

All that aside, in your case a char * is simply the address of the byte
array, so an IntPtr would work. Of course that begs the question of how to
change it to a string. Without a length, that's tough. I could post some
unsafe wackiness for getting it, but since I feel it would only lead to bad
things later on in your code, I advocate you changing the unmanaged function
instead.
 
Back
Top