Ibrahim Y

Environment:

Win XPx64

VS.Net 2005

I am forcing the compile as a 32bit only application.

I have been trying to pass a string to a C DLL. Basically the string is a file path sch as "D:\Arc2Cad\Test\MyFile.shp"

The DLL returns an error message saying the path "D" has an improper file extension.

I could send the string "ABCDEFG" and get the same error saying "A" has an improper file extension.

Obviosly the DLL is reading only the first character of the string.

I've used several forms of declare such as :

Private Declare Auto Function exec_arcv2cad Lib "D:\Arc2Cad\a2c32.dll" _

(ByVal lpCmdLine As String, ByVal Flags As Int32) As Int32

Private Declare Auto Function exec_arcv2cad Lib "D:\Arc2Cad\a2c32.dll" _

(ByVal lpCmdLine As System.Text.StringBuilder, ByVal Flags As Int32) As Int32

<DllImport("D:\Arc2Cad\a2c32.dll", BestFitMapping:=True, _

CallingConvention:=CallingConvention.Cdecl, _

SetLastError:=True, CharSet:=CharSet.Ansi)> Function init_arcv2cad _

(ByVal lpValue As String, ByVal Flags As Int32) As Long

End Function

<DllImport("D:\Arc2Cad\a2c32.dll", BestFitMapping:=True, _

CallingConvention:=CallingConvention.Cdecl, _

SetLastError:=True, CharSet:=CharSet.Ansi)> Function init_arcv2cad _

(ByVal lpValue As System.Text.StringBuilder, ByVal Flags As Int32) As Long

End Function

I've passed strings, StringBuilders, Byte arrays with a null as the last byte, etc.

No Joy Sad

But this same dll works in VBA quite nicely with this declare:

Private Declare Function exec_arcv2cad Lib "a2c32.dll" _
(ByVal lpCmdLine As String, ByVal Flags As Long) As Long

C Prototype:

typedef int(Callback* LPFN_EXEC_ARC@CAD) (

char *lpcmdline,

int Flags);

LPFN_EXEC_ARC2CAD exec_arcv2cad;

Could sure use some help here....

Thanks in advance.



Re: Visual Basic General Calling an Unmanaged C dll from VB.Net II

nobugz

Your code is acting as though the CharSet attribute should be CharSet.Ansi. The default in VB is CharSet.Auto which turns into CharSet.Unicode on any recent Windows version. Your DLL would then indeed see only the first letter. The CallingConvention attribute is almost certainly wrong, use the 1st version of your Declare statement but declare it "Ansi" instead of "Auto".





Re: Visual Basic General Calling an Unmanaged C dll from VB.Net II

Trillobyte

Hi peeps,

I have the same issue occuring, using Visual Basic, .net 2k5. i am recieving a stack imbalance, and i have tried declaring with an Ansi as per your suggestion...

Public Class linky

Declare Ansi Function OpenTest Lib "c:\link_vb.dll" Alias "Open_Test" (ByRef hdl As Long, ByVal Name As String) As Long

End Class

public sub virtual

dim lng1 as long, lng2 as long, str as string

str = c:\202222 ' this is a directory im looking for

lng1 = linky.opentest(lng2,str)

end sub

in my vba code (see green below), i would always revieve a lng1 value of 0, if the data folder was found, and a dll generated error if not (something like -5003). As it stands now in the VS.net 2k5 code, i receive a nonsensical lng1 value, a large number like 361354736986750976. Then i start pulling stack error codes as i dig deeper into my DLL functions. I have been doing VBA work for a while, and am trying to apply the same Dll calls i've used in VBA millions of times to work with visual basic...So basically, I'm newbing out here in a new environment...(my DLL is looking for a null terminated ansi string, and apparantly VBA has been providing just that). Any ideas if we are seeing the same issue

Public Declare Function OpenTest Lib "c:\link_vb.dll" Alias "Open_Test" (ByRef hdl As Long, ByVal Name As String) As Long

Sub test()
Dim lng1 As Long, lng2 As Long, str as string

str = "C:202222"
lng1 = OpenTest(lng2, str)

End Sub


don't mean to butt in, but I thought it seemed pretty darn relevant to what the topic is here...

good karma,

trill






Re: Visual Basic General Calling an Unmanaged C dll from VB.Net II

nobugz

You are butting in. Change Long to Integer.





Re: Visual Basic General Calling an Unmanaged C dll from VB.Net II

Trillobyte

didn't work. Thanks anyways. I'll go start another thread and stop butting in.






Re: Visual Basic General Calling an Unmanaged C dll from VB.Net II

Ibrahim Y

Heh....you're welcome to but in...

Declare Ansi Function OpenTest Lib "c:\link_vb.dll" Alias "Open_Test" (ByRef hdl As Long, ByVal Name As String) As Long

I notice the HDL in your declare. It's probably a window handle right

Use an IntPtr type instead of just integer or long. That way the type will adjust to the OS (32 v 64 bit)

I dont know if that will help you but it is good practice when passing handles to a function.. If you are trying to pass a fixed length string as well, use a stringbuilder as your argument instead of a string.

Ibrahim