Calling native dll function problem

Hi all,

I am using Compact framework 1.1 and VS2003

I am calling following function from "C" dll declared as

TTGPS_API void WINAPI TTGPSDetailFind(HWND hTTGPS,
UINT uiNum,
UINT uiZip,
LPTSTR strOther,
LPTSTR strStreet,
LPTSTR strPlace,
LPTSTR strState,
LPTSTR strCountry,
LPTSTR strZip,
int &rltNum,
LPTSTR rltName1,
POINT &rltLoc1,
LPTSTR rltName2,
POINT &rltLoc2,
LPTSTR rltName3,
POINT &rltLoc3
);

In VC++ application this is called as below which works successfully.

g_hWndGPS=TTGPSGetWnd();
int rltNum=-1;
TCHAR rltName[3][256];
POINT rltLoc[3];

TTGPSDetailFind(g_hWndGPS,
20,
2116,
_T(""),//LPCTSTR strOther,
_T("Park Plz"),//LPCTSTR strStreet,
_T("Boston"),//LPCTSTR strPlace,
_T("MA"),//LPCTSTR strState,
_T("United States"),//LPCTSTR strCountry,
_T("02116"),//LPCTSTR strZip
rltNum,
rltName[0],
rltLoc[0],
rltName[1],
rltLoc[1],
rltName[2],
rltLoc[2]
);

In VB.NET application, dllImport declaration is as below:

<DllImport("TTGPSAPI.dll")> _
Private Shared Sub TTGPSDetailFind(ByVal ptr As IntPtr, _
ByVal StreetNum As Integer, _
ByVal Zip As Integer, _
ByVal Other As String, _
ByVal Street As String, _
ByVal Place As String, _
ByVal State As String, _
ByVal Country As String, _
ByVal Zipstr As String, _
ByRef TotalResults As Integer, _
ByVal Result1 As String, _
ByRef Result1Point As Point, _
ByVal Result2 As String, _
ByRef Result2Point As Point, _
ByVal Result3 As String, _
ByRef Result3Point As Point)
End Sub)

And in VB.NET I am calling as below:

Dim LOCAddress1 As String

Dim LOCAddress2 As String

Dim LOCAddress3 As String

'Dim LOC_Points(3) As Point

Dim LOC_Points1 As New Point(0, 0)

Dim LOC_Points2 As New Point(0, 0)

Dim LOC_Points3 As New Point(0, 0)

TTGPSDetailFind(LOC_Handle, _

20, _

2116, _

String.Empty, _

"Park Plz".ToCharArray(), _

"Boston".ToCharArray(), _

"MA".ToCharArray(), _

"United States".ToCharArray(), _

"02116".ToCharArray(), _

LOC_ResultsReturned, _

LOCAddress1, _

LOC_Points1, _

LOCAddress2, _

LOC_Points2, _

LOCAddress3, _

LOC_Points3)

When I call like this, I get following exception:

Exception Code: 0xc0000005

Exception Address: 0x01121aa4

Writing: 0x29000000

What's wrong with the above code

Please help me.

Thanks,

Viral



Answer this question

Calling native dll function problem

  • Ginolard

    Hi Viral

    Your string marshalling seams to be wrong. Try this.

    Declaration:
    <DllImport("TTGPSAPI.dll")> _
    Private Shared Sub TTGPSDetailFind(ByVal ptr As IntPtr, _
    ByVal StreetNum As Integer, _
    ByVal Zip As Integer, _
    ByVal Other As String, _
    ByVal Street As String, _
    ByVal Place As String, _
    ByVal State As String, _
    ByVal Country As String, _
    ByVal Zipstr As String, _
    ByRef TotalResults As Integer, _
    ByVal Result1 As StringBuffer, _
    ByRef Result1Point As Point, _
    ByVal Result2 As StringBuffer, _
    ByRef Result2Point As Point, _
    ByVal Result3 As StringBuffer, _
    ByRef Result3Point As Point)
    End Sub)

    Usage:
    Dim LOCAddress1 As StringBuffer (256)
    Dim LOCAddress2 As StringBuffer (256)
    Dim LOCAddress3 As StringBuffer (256)

    Dim LOC_Points1 As New Point(0, 0)
    Dim LOC_Points2 As New Point(0, 0)
    Dim LOC_Points3 As New Point(0, 0)

    TTGPSDetailFind(LOC_Handle,
    20, _
    2116, _
    String.Empty, _
    "Park Plz", _
    "Boston", _
    "MA", _
    "United States", _
    "02116", _
    LOC_ResultsReturned, _
    LOCAddress1, _
    LOC_Points1, _
    LOCAddress2, _
    LOC_Points2, _
    LOCAddress3, _
    LOC_Points3)

    You may want to check the documentation here: http://msdn2.microsoft.com/en-us/library/e8w969hb.aspx

    Hope this helps

    Michael



  • SOTY_Programmer

    Hi Michael,

    It works now.

    Thanks a lot.

    Regards,

    Viral


  • Calling native dll function problem