我正在编写一个C++程序,它需要调用用C#编写的DLL。我按照下面的说明创建了我的C# DLL并从我的C++链接到它。
https://support.microsoft.com/en-us/kb/828736
我有一个以字符串作为参数的C#函数。如何将C++中的字符串传递到C#中?
发布于 2016-09-30 14:09:24
我找不到这个问题的简明答案,所以我把我的解决方案放在这里,希望它能对未来的人有所帮助。
TL;DR:您需要使用BSTR在C#和C++之间来回传递字符串。
我就是这么做的。
C#码
下面是我的C#代码示例。有几件事要注意:
interface
部分中被忽略。stringToPrint
参数的方式。用string
作为[MarshalAs(UnmanagedType.BStr)]
的前缀是至关重要的。string
参数。您不需要像在BSTR
中使用C++那样从C#中转换。下面将更详细地介绍这一点。.CS文件
//Reference where I got all this:
//https://support.microsoft.com/en-us/kb/828736
// Class1.cs
// A simple managed DLL that contains a method to add two numbers.
using System;
using System.Runtime.InteropServices;
namespace ManagedDLL
{
// Interface declaration.
public interface ICalculator
{
//Test functions
int Add(int Number1, int Number2);
int ReturnAge();
string StringTest();
void PrintAString([MarshalAs(UnmanagedType.BStr)] string stringToPrint);
};
// Interface implementation.
public class ManagedClass : ICalculator
{
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//Test functions
public int Add(int Number1, int Number2)
{
return Number1 + Number2;
}
public int ReturnAge()
{
return 35;
}
public string StringTest()
{
return "Can you hear me now?";
}
public void PrintAString([MarshalAs(UnmanagedType.BStr)] string stringToPrint)
{
Console.WriteLine("Trying to print a BSTR in C#");
Console.WriteLine(stringToPrint);
Console.WriteLine("Done printing");
}
}
}
C++码
在C++中需要注意以下几点:
#import
调用。这就是告诉C++如何找到您的C#库的地方。我在问题中链接到的教程中提到了这一点。AddTest()
函数。BSTR
。将std::string
转换为BSTR
相当容易,我有两个方向的转换函数。头文件
//csLink.h
#include <windows.h>
#include <iostream>
#include <string>
#import "path/to/C#/dll.tlb" raw_interfaces_only
using namespace std;
namespace Sample{
class CSLink {
public:
CSLink();
//Test functions
int AddTest(int i1, int i2);
int AgeTest();
string StringTestCall();
void stringArgTest(string s);
private:
ICalculatorPtr pCalc;
long lResult;
string convertBSTR(BSTR *s);
BSTR convertBSTR(string s);
};
}
源文件
//csLink.cpp
#include "stdafx.h"
#include "csLink.h"
using namespace std;
namespace Sample{
//Constructor
CSLink::CSLink(){
cout << "You have created a CS Link" << endl;
//https://support.microsoft.com/en-us/kb/828736
HRESULT hr = CoInitialize(NULL);
pCalc = ICalculatorPtr(__uuidof(ManagedClass));
lResult = 0;
}
//Test functions
int CSLink::AddTest(int i1, int i2){
cout << "you are adding " << i1 << " and " << i2 << endl;
pCalc->Add(i1, i2, &lResult);
cout << "The result should have been " << i1 + i2 << " and it was " << lResult << endl;
return 0;
}
int CSLink::AgeTest(){
cout << "Trying to get my age" << endl;
pCalc->ReturnAge(&lResult);
return lResult;
}
string CSLink::StringTestCall(){
BSTR s;
pCalc->StringTest(&s);
return convertBSTR(&s);
}
void CSLink::stringArgTest(string s)
{
//References I used figuring this all out:
//http://stackoverflow.com/questions/28061637/how-to-pass-string-parameters-between-c-and-c
//https://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshalasattribute(v=vs.110).aspx
//http://forums.codeguru.com/showthread.php?193852-How-to-convert-string-to-wstring
//http://stackoverflow.com/questions/6284524/bstr-to-stdstring-stdwstring-and-vice-versa
BSTR bSTR = convertBSTR(s);
cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
cout << "Testing conversion: " << convertBSTR(&bSTR) << "|end test" << endl;
pCalc->PrintAString(bSTR);
cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
}
//Utility functions
string CSLink::convertBSTR(BSTR *s){
if (*s == nullptr){
return "NULL STRING";
}
else{
wstring ws(*s, SysStringLen(*s));
string ss(ws.begin(), ws.end());
return ss;
}
}
BSTR CSLink::convertBSTR(string s){
wstring wStr = wstring(s.length(), L' ');
copy(s.begin(), s.end(), wStr.begin());
return SysAllocStringLen(wStr.data(), wStr.size());
}
}
事情就是这样。如有任何问题,我会尽力回答。
https://stackoverflow.com/questions/39800759
复制