3,248
社区成员




#ifndef _DLLTUT_DLL_H_
#define _DLLTUT_DLL_H_
#if defined DLL_EXPORT
#define DECLDIR __declspec(dllexport)
#else
#define DECLDIR __declspec(dllimport)
#endif
extern "C" //告诉编译器该部分可以在C/C++中使用。
{
DECLDIR int __stdcall Add( int a, int b );
DECLDIR void Function( void );
}
#endif
#include <iostream>
#define DLL_EXPORT
#include "testdll.h"
extern "C"
{
DECLDIR int __stdcall Add( int a, int b )
{
return( a + b );
}
DECLDIR void Function( void )
{
std::cout << "DLL Called!" << std::endl;
}
}
#pragma once
#include "resource.h" // 主符号
#include "testweb1_i.h"
#include "_Itest_opEvents_CP.h"
#if defined(_WIN32_WCE) && !defined(_CE_DCOM) && !defined(_CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA)
#error "Windows CE 平台(如不提供完全 DCOM 支持的 Windows Mobile 平台)上无法正确支持单线程 COM 对象。定义 _CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA 可强制 ATL 支持创建单线程 COM 对象实现并允许使用其单线程 COM 对象实现。rgs 文件中的线程模型已被设置为“Free”,原因是该模型是非 DCOM Windows CE 平台支持的唯一线程模型。"
#endif
using namespace ATL;
// Ctest_op
class ATL_NO_VTABLE Ctest_op :
public CComObjectRootEx<CComSingleThreadModel>,
public CComCoClass<Ctest_op, &CLSID_test_op>,
public IConnectionPointContainerImpl<Ctest_op>,
public CProxy_Itest_opEvents<Ctest_op>,
public IObjectWithSiteImpl<Ctest_op>,
public IDispatchImpl<Itest_op, &IID_Itest_op, &LIBID_testweb1Lib, /*wMajor =*/ 1, /*wMinor =*/ 0>
{
public:
typedef int (__stdcall*AddFunc)(int,int); //类型定义,对应DLL ADD方法。Func自定义,随便写。
HINSTANCE hInstLibrary;
AddFunc _AddFunc; //类映射
Ctest_op()
{
//开始调用DLL,进行计算。
hInstLibrary = ::LoadLibrary(L"C:\\Users\\xia0sheng\\Desktop\\ceshi\\dll_test1\\Debug\\dll_test1.dll");//把写好的dll_test.dll文件放在此项目生成的目录debug下。
//小哆觉得这应该就是相对路径的成功应用了。以后打包时,因为一直在同一路径下,所以,只要同时移动就不会出问题。
if (hInstLibrary == NULL)
{
FreeLibrary(hInstLibrary);//资源释放
}else{
}
//调用方法,返回方法句柄。
_AddFunc = (AddFunc)GetProcAddress(hInstLibrary, "Add");
}
DECLARE_REGISTRY_RESOURCEID(IDR_TEST_OP)
BEGIN_COM_MAP(Ctest_op)
COM_INTERFACE_ENTRY(Itest_op)
COM_INTERFACE_ENTRY(IDispatch)
COM_INTERFACE_ENTRY(IConnectionPointContainer)
COM_INTERFACE_ENTRY(IObjectWithSite)
END_COM_MAP()
BEGIN_CONNECTION_POINT_MAP(Ctest_op)
CONNECTION_POINT_ENTRY(__uuidof(_Itest_opEvents))
END_CONNECTION_POINT_MAP()
DECLARE_PROTECT_FINAL_CONSTRUCT()
HRESULT FinalConstruct()
{
return S_OK;
}
void FinalRelease()
{
}
public:
STDMETHOD(GetContent)(LONG a, LONG b, LONG* out);
};
OBJECT_ENTRY_AUTO(__uuidof(test_op), Ctest_op)
[#include "stdafx.h"
#include "test_op.h"
#include <iostream>
using namespace std;
// Ctest_op
STDMETHODIMP Ctest_op::GetContent(LONG a, LONG b, LONG* out)
{
// TODO: 在此添加实现代码
int sum = _AddFunc(static_cast<int>(a),static_cast<int>(b));
*out = long(sum);
return S_OK;
}/code]
第三个项目,是调用制作成的active 项目名称是验证,源代码如下:
[code=c]#include "stdafx.h"
#include "C:\Users\xia0sheng\Desktop\ceshi\testweb1\testweb1\testweb1_i.h"
#include "C:\Users\xia0sheng\Desktop\ceshi\testweb1\testweb1\testweb1_i.c"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
HRESULT hr;
Itest_op *IFirstATL = NULL;
// Now we will intilize COM
hr = CoInitialize(0);
// Use the SUCCEDED ro and see if we can get a pointer to
// the interface
if(SUCCEEDED(hr))
{
hr = CoCreateInstance( CLSID_test_op, NULL, CLSCTX_INPROC_SERVER, IID_Itest_op, (void**) &IFirstATL);
// If we succeeded then call the AddNumbers method, if it failed
// then display an appropriate message to the user.
if(SUCCEEDED(hr))
{
long ReturnValue;
hr = IFirstATL->GetContent(5, 7, &ReturnValue);
cout << "The answer for 5 + 7 is: " << ReturnValue << endl;
hr = IFirstATL->Release();
}
else
{
cout << "CoCreateInstance Failed." << endl;
}
}
// Uninitialize COM
CoUninitialize();
system("pause");
return 0;
}