你能告诉我如何在 vc++ 项目或库中调用 c# dll。这是我的 c# dll 代码,它工作正常,我只想从 vc++ 项目中调用这个 dll,但是当我从 vc++ 调用这个 dll 时,我没有得到任何响应。
我的 c# dll 代码是-
namespace MyInterop
{
[Guid("3E0E2EB2-CC13-40fb-9346-34809CB2418C")]
public interface IMyDotNetInterface
{
void NewFile(string a, string b);
}
[ClassInterface(ClassInterfaceType.None)]
[Guid("3A13EB34-3930-4e06-A3EB-10724CCC2F4B")]
public class MyDotNetClass:IMyDotNetInterface
{
public MyDotNetClass ()
{
}
public void NewFile(string a, string b)
{
string c= a + b;
string fileName = @"D:\file.txt";
try
{
// Check if file already exists. If yes, delete it.
if (File.Exists(fileName))
{
File.Delete(fileName);
}
// Create a new file
using (FileStream fs = File.Create(fileName))
{
// Add some text to file
TextWriter tsw = new StreamWriter(@"D:\Hello.txt");
//Writing text to the file.
tsw.WriteLine(c);
tsw.Close();
}
}
catch (Exception Ex)
{
}
}
我的 vc++ 代码是-
BSTR param1 = L"hello";
BSTR param2 = L"World";
CoInitialize(NULL);
MyInterop::IMyDotNetInterfacePtr pDotNetCOMPtr;
HRESULT hRes = pDotNetCOMPtr.CreateInstance(MyInterop::CLSID_MyDotNetClass);
if (hRes == S_OK)
{
pDotNetCOMPtr->NewFile(param1,param2);
}
请建议我