Hi all,
Greetings! this is my first post to the forum.
My question is the following. I have a c++ code which, after being compiled as a console application (I'm using DEV-C++ 4.9.9.2), the code does what I expect it to do.
However, if I compile the same code as a DLL and after that I call it from within Excel (using Excel 2007), then it will crash.
I have no problem in using DLLs from within Excel; I've done that before and the work as a charm. The problem arises with this code and I cannot figure out what is wrong.
I want to bring up this: if compiled as a console application it works; if compiled as a DLL and called from Excel, Excel crashes.
Here's the code
Code:
#include <windows.h>
#include <string>
using namespace std;
extern "C" __declspec( dllexport ) __stdcall string str_separator(string in_string, string sep_char, int id);
string str_separator(string in_string, string sep_char, int id)
{
int str_len=0,pos_sep_char=0,q=0,k=0;
// temporal strings declared as 'string' types.
string temp_str ("test_str");
string out_str ("out_str");
string temp_char ("0");
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==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 != sep_char) // gotten char is not the one
{
if (k==0)
{out_str = temp_char;}
else
{out_str += temp_char;}
}
}
return out_str;
}
Any hint is highly appreciated.
Best regards,
Lucas