在c中调用c#中的dll

时间:2013-05-31 14:41:43

标签: c# c com

我在c#中写了一个dll,现在我想在c中使用这个dll中的一个函数 我可以在c。中创建该对象 但我无法调用函数,例如:hallo()。

有没有其他方法可以在c中导入dll / com?

的main.c

#include <windows.h>
#include <objbase.h>
#include <stdio.h>
#include "IExample.h"


int main(int argc, char **argv)
{
IExample        *exampleObj;
HRESULT         hr;

if (!CoInitialize(0))
{
    hr = CoCreateInstance(&CLSID_IExample, 0,CLSCTX_INPROC_SERVER, &IID_IExample, &exampleObj);
    if (hr==S_OK)
    {
         exampleObj->lpVtbl->hallo(exampleObj); //**didnt call the function hallo**
        exampleObj->lpVtbl->Release(exampleObj);
    }
    else
    {
        MessageBox(0, "Can't create IExample object",
          "CoCreateInstance error",
          MB_OK|MB_ICONEXCLAMATION);

    }

    CoUninitialize();
}
else
{
    MessageBox(0, "Can't initialize COM", "CoInitialize error", MB_OK|MB_ICONEXCLAMATION);
    return 1;
}


return 0;
    }

IExample.h

#ifndef _IEXAMPLE_H_
#define _IEXAMPLE_H_

#include <initguid.h>

DEFINE_GUID(CLSID_IExample, 0x0C8C7C5A, 0x729F, 0x43A4, 0x90, 0x06, 0x57, 0x7B, 0xF6, 0x27, 0xFA, 0x75);

DEFINE_GUID(IID_IExample  , 0xD685CD6F, 0x5D76, 0x4868, 0x9C, 0xE4, 0x8A, 0xBB, 0xF6, 0x0A, 0xCA, 0x06);

#undef  INTERFACE
#define INTERFACE   IExample
DECLARE_INTERFACE_ (INTERFACE, IUnknown)
{
STDMETHOD  (QueryInterface)     (THIS_ REFIID, void **) PURE;
STDMETHOD_ (ULONG, AddRef)      (THIS) PURE;
STDMETHOD_ (ULONG, Release)     (THIS) PURE;
STDMETHOD_  (BOOL, hallo)           (THIS) PURE;

};

#endif // _IEXAMPLE_H_

的Class1.cs

using System;
using System.Runtime.InteropServices;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Forms;

namespace fb_dll
{
[Guid("D685CD6F-5D76-4868-9CE4-8ABBF60ACA06")]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
[ComVisible(true)]
public interface DBCOM_Interface
{
    [DispId(1)]
    bool hello();
} 


[Guid("0C8C75A-729F-43A4-9006-577BF627FA75")]
[ClassInterface(ClassInterfaceType.AutoDual)]
[ComVisible(true)]
public class DBCOM_Class : DBCOM_Interface
{
    public DBCOM_Class()
    {
        //Init();
        //MessageBox.Show("jauaua");
    }

    public bool hello()
    {
        MessageBox.Show("jauaua");
        return true;
    }
}

}

0 个答案:

没有答案