如何将C ++控制台参数发送到C#DLL [英] How to send C++ console params to C# DLL

查看:122
本文介绍了如何将C ++控制台参数发送到C#DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将两个作为参数输入或由C ++控制台请求的数字发送到C#中的库。



手动代码为:

I need to send two numbers entered as parameters or requested by C++ console to a library in C#.

Manually the code is:

BSTR thing_to_send = ::SysAllocString(L"10 20");





如何使用控制台指示的参数值或类型的两个变量创建BSTR类型的变量整数或字符串。



我需要使用它们之间的空格来连接值,例如:



string Num1 = 30;

string Num2 = 40;

string dataToSend = Num1 ++ Num2;







string Num1 = argv [1];

string Num2 = argv [2];

string dataToSend

dataToSend + = Num1 +;

dataToSend + = Num2;



如何我可以将dataToSend转换为BSTR有效变量以发送:

dataToSend to BSTR?



我尝试了什么:



在每一页中我已经回顾了转换的其他类型的原点值,其中链的显式值类似于转换为Cade而不是使用变量,因为在这种情况下是



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

推荐答案

如果你需要它直接用C ++而不是 _bstr_t 应该去。否则我更喜欢分配一些缓冲区而不是复制字符串,如我的文章所示
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.


我从用户@devdimi的另一个论坛获得解决方案



BSTR CombineStrings(_TCHAR * arg1,_TCHAR * arg2){

long len1 = wcsnlen_s(arg1,100);

long len2 = wcsnlen_s(arg2,100);



BSTR结果= SysAllocStringLen(NULL,len1 + len2 + 1);

memcpy(结果,arg1,len1 * sizeof(OLECHAR));

memcpy(结果+ len1,L,1 * sizeof(OLECHAR));

memcpy(结果+ len1 + 1,arg2,len2 * sizeof(OLECHAR));



结果[len1 + len2 + 1] = NULL; //包含firstarg< empty> secondarg

返回结果;

}



int _tmain(int argc,_TCHAR * argv [])

{

if(argc< 3){

//需要两个参数。

返回-1;

}



BSTR combinedStr = CombineStrings(argv [1],argv [2]);

BSTR returned_thing;

HRESULT hResult = obj-> GetTheThing(combinedStr,& returned_thing);



SysFreeString(combinedStr);

返回0;

}
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;
}


这篇关于如何将C ++控制台参数发送到C#DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆