sending string data from custom dll to vb.net CF

A

Andrew Jones

Hi,

I'm trying to send data via a custom dll created in embedded C++ and use the
dll in VB.Net Compact Framework for the PDA.

I can pass boolean values but for some reason my string value isn't being
passed. It's just null on the VB.NET CF side.

Below is a sample DLL's code in embedded C++:

-----------------------------------START

#include "stdafx.h"

BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}


extern "C"
{

__declspec(dllexport) bool Test(BSTR testStr)
{
wsprintf(testStr, _T("This is a test. [%S]"), __FILE__);

return true;
}

}
----------------------------------------END


Below is my VB.NET CF Code. The 1st message box returns the correct boolean
value ok but the 2nd one shows null for the string.

---------------------------------------START
<DllImport("test.dll")> _

Private Function Test(ByVal thiswillwork As String) As Boolean

End Function

-------

Dim strTeststring As String

Dim blnYESORNO As Boolean

blnYESORNO = Test(strTeststring)

MsgBox(blnYESORNO)

MsgBox(strTeststring)

===================================END

Thanks for any help!

Andrew Jones
 
R

Rick Winscot

Andrew,

What you have here is a pointer issue... that falls into the cracks of
marshaling. Sample code below...

(PInvoke in your header)
<DllImport("iPaqAssets.dll")> _

Public Shared Function GetSerial(ByRef pHandle As IntPtr) As Int32

End Function

(code inside button to call the function - note the marshaling of a
pointer to a string)

Try

Dim pHandleIn As IntPtr

Dim pFunctionRetVal As Int32

Dim sReturnString As String



pFunctionRetVal = GetSerial(pHandleIn)

sReturnString = Marshal.PtrToStringUni(pHandleIn)

MsgBox("Serial: " & sReturnString, MsgBoxStyle.Information,
"Device Serial Number")



Catch ex As Exception

MsgBox("Error: " & ex.Message, MsgBoxStyle.Information,
"Exception Occured")

End Try





(C++ code...)



__declspec(dllexport) void GetSerial(BSTR* pString)

{



TCHAR tResult[20];

HINSTANCE hDll;

typedef BOOL (CALLBACK *LPFNiPAQGetSerialNumber)
(TCHAR *);

LPFNiPAQGetSerialNumber iPAQGetSerialNumber;

hDll = LoadLibrary(TEXT("iPAQUtil.dll"));

iPAQGetSerialNumber =
(LPFNiPAQGetSerialNumber)GetProcAddress(hDll, TEXT("iPAQGetSerialNumber"));

iPAQGetSerialNumber(tResult);



SysFreeString(*pString);

*pString = SysAllocString(tResult);



}


I just submitted an article to www.devbuzz.com that walks through the entire
process... with a video tutorial. If you don't see it up in the next day or
so - drop me a line and I will send you all the source.

Rick Winscot
rickly@zyche dot com
 

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