0

i'm a newbie in c++ and tring to create a dll with c++ which will be accessed from excel vba,inside the dll i hve a function which will take string as argument and modify the string and atlast the modified string will be used in the vba.

the steps once more :

1.a dll with a function with string pointer as an argument.

2.vba empty string will be passed to the function.

3.the dll will modify or manipulate the string.

4.finally the modified string will be used in vba.

the function inside the dll is somewhat like:

extern "C"_declspec(dllexport) void WINAPI ModifyStr(char* str) { Str = "Hello" } the following error is thrown by the compiler "deprecated conversion from string constant to char*"

i modified the function by writing

_declspec(dllexport) void WINAPI ModifyStr(string** Str) { *Str = new string("hello"); } this code is compiled well but when i tried to call the function from vba it resulted in program crash.Please help me get rid of this problem and build my dll without any error.

1 Answer 1

0

From MSDN

A VBA String is passed as a pointer to a byte-string BSTR structure when passed ByVal, and as a pointer to a pointer when passed ByRef

In your dll you should use BSTR str = SysAllocString(L"Hello World!"); to allocate the string. COM memory mangement should handle deallocation.

Sign up to request clarification or add additional context in comments.

1 Comment

@user14297 The type of the string passed to your function is not char* nor string** but BSTR

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.