VBA calls the same C ++ dll as other results

I have a test DLL function that reads a string and displays it:

int _stdcall test(LPSTR myString) { MessageBoxA(NULL, (LPCSTR)myString, "test", MB_OK); return 0;} 

Excel vba ad

 Declare Function test Lib "test.dll" (ByVal text As String) As Long 

There is a sub function that calls the function. It reads an excel cell with the value "abcde" and can display the correct result. Subcode:

 sub testCode() test (cells(1,1).value) end sub 

However, when you call the dll function using the formula excel =test(A1) , only the first char "a" line is displayed in the message box.

I spent the whole weekend reading BSTR, etc., I still can’t solve it. What's going on here?

+3
source share
2 answers

Declare the imported function as confidential:

 Private Declare Function test_internal Lib "test.dll" Alias "test" (ByVal text As String) As Long 

and create a wrapper function for Excel:

 Public Function Test (ByVal text As String) As Long Test = test_internal(text) End Functin 

Since, apparently, while VB (A) converts string arguments to ASCII using the current system code page when calling the Declare d API, the Excel engine does not work.

On a side note, you can also remove parentheses .

+2
source

Excel passes the BSTR C ++ code. Each character is encoded with 2 bytes, the second - 0 for regular characters. This explains why you only see the first, because MessageBoxA expects a char * null-terminated string. Use

MessageBox W

Try with this code.

 int _stdcall test( const wchar_t * pString ) { MessageBoxW( NULL, pString, L"test", MB_OK ); // note the L prefix for literral return 0; } 

Alternatively, you can translate the BSTR pointer to char * one using some ATL macros or the raw Win32 APIs as WideCharToMultiByte (more complex, easy to use ATL)

+1
source

Source: https://habr.com/ru/post/955372/


All Articles