Hi,
I have created my own dll in c++ to use in MapInfo. The exportable function takes a string parameter and returns a string value. Example below.
BSTR __declspec(dllexport) __stdcall SayHello(BSTR BSTR_str)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
CString CString_str = _T("");
//This converts the BSTR to CString
if (BSTR_str != NULL)
{
CString s;
LPSTR p = s.GetBuffer(::SysStringLen(BSTR_str) + 1);
BOOL UsedDefaultChar;
::WideCharToMultiByte(CP_ACP, 0, BSTR_str, -1, p, ::SysStringLen(BSTR_str)+1, NULL, &UsedDefaultChar);
if (UsedDefaultChar)
{
CString_str = (LPCTSTR)BSTR_str;
}
else
{
CString_str = (LPCWSTR)BSTR_str;
}
}
CString_str = “Hello there ” + CString_str;
return CString_str.AllocSysString(); //Which return a BSTR type
}
Now in the mbx program, I’m trying to retrieve that string value, in VB you could convert it but I’m not sure how in MapBasic, simply placing it into a string variable will give me the first letter.
Should I be doing it this way or can I return some other type that I can retrieve easier?
Any ideas or suggestions would be greatly appreciated!
Regards,
Hayden Fisher
Hi Uffe,
Just a couple of questions, still brushing up on my c++, is LPTSTR a zero-terminated buffer and is CString unicode?
To return that pointer how does this look and I know this won’t quite work but am I close at all?
LPTSTR __declspec(dllexport) __stdcall SayHello()
{
LPTSTR myBuffer;
CString myReturnValue = “some text”;
myBuffer = myReturnValue.GetBuffer(0);
return &myBuffer;
}
Regards,
Hayden Fisher
Development and Support
BizeAsset Pty Ltd
Mob: (04) 3838 4747
Ph: (07) 3369 2222
Toll Free: 1800 836 850
Hi,
Ok I’ve done some changes to the dll and it is passing back the pointer no problem. However what is happening is that once the function returns, the string that was created in the function, its destructor is called and what I’m feeding into my string variable back in MB is rubbish.
So looking at your example (and I don’t know delphi) you declare your string as a global variable ensuring that it doesn’t disappear when the function ends???
And this is where I get confused, because you have the pointer sent back, it is able to free up that memory as soon as the MB program has finished with (as in your example) the ‘a’ or ‘b’ variables, say if you leave the sub routine.
Uffe
Any comments welcome
Regards,
Hayden
MapBasic calling of DLL functions does not support BSTR which is a COM type which is Unicode. The data on the DLL end must be single byte (char in C/C++)!
Eric Blasenheim
Chief Product Architect
Pitney Bowes Business Insight [MapInfo]
Mail List:grbounce-YvY1eQUAAAAJBprYSySRydkk7vpghP_9=mail_list=mapin...@googlegroups.com
|
From: |
Hayden Fisher <sup...@bizeasset.com.au> on 04/03/2009 03:51 PM ZE10 |
|
To: |
|
|
cc: |
|
Otherwise I think there are plenty of other free c compilers around, if
the 40 euro is too much for anyone.
Regards
Uffe Kousgaard