Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to send two numbers entered as parameters or requested by C++ console to a library in C#.

Manually the code is:
C++
BSTR thing_to_send = ::SysAllocString(L"10 20");


How can I create a variable of type BSTR using the values of the parameters indicated by the console or by two variables of type integer or string.

I need to concatenate the values using a space between them, for example:

string Num1 = 30;
string Num2 = 40;
string dataToSend = Num1 + " " + Num2;

or

string Num1 = argv[1];
string Num2 = argv[2];
string dataToSend
dataToSend += Num1 + " ";
dataToSend += Num2;

How I can convert dataToSend to BSTR valid variable to send with:
dataToSend to BSTR?

What I have tried:

In each page that I have reviewed other types of values of origin for the transformation are indicated, with explicit values of chain like being "Cade to convert" but not with the use of variables as it is in this case
Posted
Updated 16-Nov-18 0:26am

If you need it direct in C++ than _bstr_t should go. Else I prefer to allocate some buffer and than copy the string as demonstrated in that article of mine.
 
Share this answer
 
Comments
fisadmaster 15-Nov-18 9:46am    
I really know what I need, what I have asked is how to achieve it? The article does not show a conversion that satisfies the question made here.
KarstenK 16-Nov-18 3:12am    
_bstr_t bstr1(_T("This is the test string.")); doesnt work for using bstr1 for c#?
I get the solution from another forum from user @devdimi

BSTR CombineStrings(_TCHAR* arg1, _TCHAR* arg2) {
long len1 = wcsnlen_s(arg1, 100);
long len2 = wcsnlen_s(arg2, 100);

BSTR result = SysAllocStringLen(NULL, len1 + len2 + 1);
memcpy(result, arg1, len1 * sizeof(OLECHAR));
memcpy(result + len1, L" ", 1 * sizeof(OLECHAR));
memcpy(result + len1 + 1, arg2, len2 * sizeof(OLECHAR));

result[len1 + len2 + 1] = NULL; // contains "firstarg<empty>secondarg"
return result;
}

int _tmain(int argc, _TCHAR* argv[])
{
if (argc < 3) {
// two arguments required.
return -1;
}

BSTR combinedStr = CombineStrings(argv[1], argv[2]);
BSTR returned_thing;
HRESULT hResult = obj->GetTheThing(combinedStr, &returned_thing);

SysFreeString(combinedStr);
return 0;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900