从C ++调用未注册的.net dll. [英] Calling unregistered .net dll from c++.

查看:52
本文介绍了从C ++调用未注册的.net dll.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

如何在C ++中调用在.net Framework 2.0中创建的dll,
尽管dll未进行COM注册.通过注册dll,我可以使用以下代码调用dll.

Hello

How do i call a dll which is created in .net framework 2.0 in C++,
although the dll is not COM registered. With registering the dll i can call the dll with the following code.

#include "stdio.h"
#include "conio.h"

#import "C:\Documents and Settings\Desktop\d\test.dll.tlb"

using namespace test;

#include <string>;
using namespace std;

void main(int argc, char* argv[])
{
  HRESULT hr = CoInitialize(NULL);
  IPayProcess* pTest = NULL;
  hr = CoCreateInstance(__uuidof(Pay), NULL, CLSCTX_INPROC_SERVER, __uuidof(IPayProcess), (void**)&pTest);
  pTest->;PayProcess("abc");
}
</string>



如果我不注册该dll,它将给出未设置为实例错误的对象引用.

谢谢



If I don''t register the dll it gives the object reference not set to an instance error.

Thanks

推荐答案

看看免费注册的COM互操作性:
http://msdn.microsoft.com/en-us/library/fh1h056h.aspx [ ^ ]

还有DllImportAttribute类:
http://msdn.microsoft.com/en-us/library/system. runtime.interopservices.dllimportattribute.aspx [ ^ ]

祝您好运!
Have a look Registration-Free COM Interop:
http://msdn.microsoft.com/en-us/library/fh1h056h.aspx[^]

And also the DllImportAttribute Class:
http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.dllimportattribute.aspx[^]

Good luck!


您可以使用CoLoadLibrary()加载dll并使用以下代码获取对它的引用;只需将dll的路径添加到第一个参数即可.还要将对CLSID_CGenericIID_IGeneric的引用更改为您自己的GUID引用.

请注意,在尝试使用指针之前,您应该始终检查CoCreateInstance()中的返回代码.

You can use CoLoadLibrary() to load the dll and obtain a reference to it with the following code; just add the path to your dll to the first parameter. Also change references to CLSID_CGeneric and IID_IGeneric to your own GUID references.

Note that you should always check the return code from CoCreateInstance() before trying to use the pointer.

// check the return code
if (hr == REGDB_E_CLASSNOTREG)
{
    // try CoLoadLibrary...
    HINSTANCE hInstance = CoLoadLibrary([path to your DLL], FALSE);
    if (hInstance != NULL)
    {
        typedef HRESULT (__stdcall *pDllGetClassObject)(REFCLSID rclsid, REFIID riid, PVOID* ppv);
			
        pDllGetClassObject GetClassObject = (pDllGetClassObject)GetProcAddress(hInstance, "DllGetClassObject");
        if (GetClassObject != NULL)
        {
            IClassFactory*	pIFactory;

            hr = GetClassObject(CLSID_CGeneric, IID_IClassFactory, (PVOID*)&pIFactory);

            if (SUCCEEDED(hr))
            {
                hr = pIFactory->CreateInstance(NULL, IID_IGeneric, (PVOID*)&pGeneric);
                pIFactory->Release();
            }
        }
    }
}
if (SUCCEEDED(hr))
{
    // use the COM object as required
}


这篇关于从C ++调用未注册的.net dll.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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