I have a BSTR type variable (passed in from a Visual Basic application) that I'm trying to convert into a char* so that I can use it in an iostream. For some reason, the conversion function that I'm using only converts every SECOND character of the BSTR variable to the char variable. For example, if I have:
BSTR a = "anystring";
char* c;
c = BSTRtoChar(a);
then I get c = "aytig". Why?????? Here is my full code:
BSTR a = "anystring";
char* c;
c = BSTRtoChar(a);
then I get c = "aytig". Why?????? Here is my full code:
Code:
void __declspec(dllexport) CALLBACK myfunction(BSTR mybstr)
{
int n;
char* mychar;
n = SysStringLen(mybstr); [COLOR=green]// length of input string[/color]
mychar = (char*) malloc(n+1); [COLOR=green]// allocate space to character array[/color]
[b]mychar=BSTRtoChar(mybstr);[/b] [COLOR=green]// Convert VB string to C++ char array.[/color]
ifstream infile1(mychar);
...etc...
}
[COLOR=green]// BSTR to Char conversion function:[/color]
char* BSTRtoChar(BSTR String)
{
int n, i;
char* FinalChar;
n = SysStringLen(String); [COLOR=green]// length of input[/color]
FinalChar = (char*) malloc(n+1);
for (i = 0; i < n; i++)
{
FinalChar[i] = (char) String[i];
}
FinalChar[i] = 0;
return FinalChar;
}