C++ -VS2022-创建ATL-COM组件动态库被其它程序调用2-附源码,亲测可行。

2024-01-03 11:32:47

上篇写的是.dll
本篇写调用它的.exe
1.以管理员身份,把dll动态库注册到注册表中 .



2.调用DLL的exe

2.1新建项目MFC应用-基于对话框:




2.2 添加dll的.c和.h文件到exe文件
#include "UnifiedRegistInterface_i.c"
#include "UnifiedRegistInterface_i.h"

void CATLTestDlg::OnBnClickedButton1()
{
	HRESULT hr = S_OK;
	hr = CoInitialize(NULL);

	ITypeLib* pTypeLib = NULL;
	hr = ::LoadTypeLib(L"D:\\Source\\repos\\ATLTest\\UnifiedRegistInterface.dll", &pTypeLib);
	if (FAILED(hr))
	{
		return;
	}

	int nTypeInfoCount = pTypeLib->GetTypeInfoCount();
	if (nTypeInfoCount <= 0) //返回类型库中的类型说明的数量
	{
		return;
	}

	ITypeInfo* pTypeInfo = NULL;
	TYPEATTR* pTypeAttr = NULL;

	//!!!此处一定要注意导出类在.idl文件中的顺序!!!
	hr = pTypeLib->GetTypeInfo(6, &pTypeInfo);
	hr = pTypeInfo->GetTypeAttr(&pTypeAttr);
	CLSID clsid = pTypeAttr->guid;

	hr = pTypeLib->GetTypeInfo(6, &pTypeInfo);
	hr = pTypeInfo->GetTypeAttr(&pTypeAttr);
	IID iid = pTypeAttr->guid;

	IScadantRegister* pScadaRegister = NULL;
	hr = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER, iid, (void**)&pScadaRegister);
	hr = CoCreateInstance(CLSID_ScadantRegister, NULL, CLSCTX_INPROC_SERVER, IID_IScadantRegister, (void**)&pScadaRegister);

	
	BSTR* retstr = NULL;
	BSTR PutInstr = SysAllocString(L"2023 - 12 - 18 | V20 |7226526141");
	pScadaRegister->getRegString(PutInstr, retstr);

	pTypeInfo->ReleaseTypeAttr(pTypeAttr);
	pTypeInfo->Release();
	CoUninitialize();

}


示例代码:
https://download.csdn.net/download/aggie4628/88694018

https://download.csdn.net/download/aggie4628/88694013

文章来源:https://blog.csdn.net/aggie4628/article/details/135339398
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。