C++ console application works; Excel crashes when using the same code.

Hi all! Greetings, this is my first post to the forum.

I'm having the following problem. I have a code that compiles well as a console application and does what I expect it to do (I'm using DEV-C++ 4.9.9.2).

I tried to compile the same code later on as a DLL to call it from within VBA in Excel, but Excel would crash.

After some investigation I arrived to the conclusion that Excel did not like the string type I was using in the c++ code.

Found out as well that Excel uses a string type which is compatible with the C++ BSTR type. However I cannot still send strings from Excel to the C++ DLL.

The thing is that I'd like to keep on using the <string> library since I find it really useful when working with strings within c++.

Please, find out my -not yet working- code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <windows.h>
#include <string>
#include <cstring>

extern "C" __declspec( dllexport ) __stdcall BSTR str_separator(BSTR in_string, BSTR sep_char, int id);

BSTR str_separator(BSTR in_string, BSTR sep_char, int id)
{
     int str_len=0,pos_sep_char=0,q=0,k=0;
     // temporal strings declared as 'CString' types.
     string temp_str ("test_str");
     string temp_char ("0");
     string out_str ("out_str");
     string temp_sep_char;
     char *c_out_str;
     
     temp_sep_char = sep_char;
     temp_str = in_string;
     str_len=temp_str.length();
     
     for(k=0;k<=str_len-1;k++)
     {
           // here we get the kth char from the string to compare it after
           temp_char = temp_str.substr(k,1);
           
           if (temp_char==temp_sep_char) // is the gotten char the one I'm expecting?
           {
                // I got sep_char within the string.
                q=q+1;
                
                if (id==q)
                {k=str_len;}
                else 
                {
                     temp_str = temp_str.substr(k+1,temp_str.length()-k);
                     str_len=temp_str.length();
                     k=-1;
                }
           }
           else if (temp_char != temp_sep_char) // gotten char is not the one
           {
                if (k==0)
                {out_str = temp_char;}
                else
                {out_str += temp_char;}
           }
     }
     
     // Since VB uses a string type including
     // - header with length
     // - data (with 2 bytes per char)
     // then we must convert the c++ string into BSTR using the AllocSysString()
     strcpy (c_out_str,out_str.c_str());
     return SysAllocStringByteLen(c_out_str,lstrlen(c_out_str));
}



The code above will not compile now. If declare the function as follows and I do not use the SysAllocStringByteLen function at the end, then it does compile but Excel crashes.

extern "C" __declspec( dllexport ) __stdcall string str_separator(string in_string, string sep_char, int id);

I'm lost.

Any hint is highly appreciated.

Best regards,

Lucas
Topic archived. No new replies allowed.