在vc ++项目或库中调用c#dll

时间:2012-12-01 10:37:29

标签: visual-c++ c#-3.0

请告诉我如何在vc ++项目或库中调用c#dll。这是我的c#DLL代码,它工作正常,我只是想调用这个dll形式的vc ++项目,但是当我从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);

}

请建议我

0 个答案:

没有答案