.CPP
// HotelImp.cpp : CHotelImp 的实现
#include "stdafx.h"
#include <comdef.h>
#include "HotelImp.h"
#include "HotelF.h"
#include "slog.h"
#pragma warning(disable : 4996)
// CHotelImp
//typedef int(*InitF)(int software, char *server, char *username, int port, int Encoder, int TMEncoder);
STDMETHODIMP CHotelImp::Init(VARIANT software, VARIANT server, VARIANT username, VARIANT port, VARIANT encoder, VARIANT TMEncoder, BSTR* vaR)
{
// TODO: 在此添加实现代码
if (hDll == nullptr)
{
return -100000; //dll加载失败
}
long lsoftware = software.lVal;
_bstr_t _bstr_server(server.bstrVal);
char* lserver = _bstr_server;
_bstr_t _bstr_username(username.bstrVal);
char* lusername = _bstr_username;
long lport = port.lVal;
long lencoder = encoder.lVal;
long lTMEncoder = TMEncoder.lVal;
int iR = -1111;
InitF InitFun = (InitF)GetProcAddress(hDll, "Init"); //"Init"是DLL里的函数名
if (InitFun != NULL)
{
iR = InitFun(lsoftware, lserver, lusername, lport, lencoder, lTMEncoder);
}
char *pR = new char[4];
_itoa(iR, pR, 10);
_bstr_t _bstr_r(pR);
BSTR bstr = _bstr_r.copy(true);
*vaR = bstr;
delete[]pR;
pR = nullptr;
return S_OK;
}
//typedef int(*ReadCardF)(char *room, char *gate, char *stime, char *guestname, char *guestid, char *lift, char *track1, char *track2, long *cardno, int *st, int *Breakfast);
STDMETHODIMP CHotelImp::ReadCard(VARIANT * room, VARIANT * gate, VARIANT * stime, VARIANT * guestname, VARIANT * guestid, VARIANT * lift, VARIANT * track1, VARIANT * track2, VARIANT * cardno, VARIANT * st, VARIANT * breakfast, BSTR* vaR)
{
// TODO: 在此添加实现代码
if (hDll == nullptr)
{
return -100000; //dll加载失败
}
char *croom = new char[30];
char *cgate = new char[30];
char *cstime = new char[30];
char *cguestname = new char[30];
char *cguestid = new char[30];
char *clift = new char[30];
char *ctrack1 = new char[30];
char *ctrack2 = new char[30];
long iccardno = 0;
int ist = 0;
int iBreakfast = 0;
int iR = -1111;
ReadCardF ReadCardFun = (ReadCardF)GetProcAddress(hDll, "ReadCard"); //"ReadCard"是DLL里的函数名
if (ReadCardFun != NULL)
{
iR = ReadCardFun(croom, cgate, cstime, cguestname, cguestid, clift, ctrack1, ctrack2, &iccardno, &ist, &iBreakfast);
}
VARIANT vacroom;
VariantInit(&vacroom);
vacroom.vt = VT_BSTR;
_bstr_t bstr_room(croom);
BSTR bstr = bstr_room.copy(true);
vacroom.bstrVal = bstr;
VARIANT vacgate;
VariantInit(&vacgate);
vacgate.vt = VT_BSTR;
_bstr_t bstr_gate(cgate);
BSTR bstrgate = bstr_gate.copy(true);
vacgate.bstrVal = bstrgate;
VARIANT vacstime;
VariantInit(&vacstime);
vacstime.vt = VT_BSTR;
_bstr_t bstr_stime(cstime);
BSTR bstrstime = bstr_stime.copy(true);
vacstime.bstrVal = bstrstime;
VARIANT vacguestname;
VariantInit(&vacguestname);
vacguestname.vt = VT_BSTR;
_bstr_t bstr_cguestname(cguestname);
BSTR bstrguestname = bstr_cguestname.copy(true);
vacguestname.bstrVal = bstrguestname;
VARIANT vacguestid;
VariantInit(&vacguestid);
vacguestid.vt = VT_BSTR;
_bstr_t bstr_cguestid(cguestid);
BSTR bstrcguestid = bstr_cguestid.copy(true);
vacguestid.bstrVal = bstrcguestid;
VARIANT vaclift;
VariantInit(&vaclift);
vaclift.vt = VT_BSTR;
_bstr_t bstr_clift(clift);
BSTR bstrclift = bstr_clift.copy(true);
vaclift.bstrVal = bstrclift;
VARIANT vactrack1;
VariantInit(&vactrack1);
vactrack1.vt = VT_BSTR;
_bstr_t bstr_ctrack1(ctrack1);
BSTR bstrctrack1 = bstr_ctrack1.copy(true);
vactrack1.bstrVal = bstrctrack1;
VARIANT vactrack2;
VariantInit(&vactrack2);
vactrack2.vt = VT_BSTR;
_bstr_t bstr_ctrack2(ctrack2);
BSTR bstrctrack2 = bstr_ctrack2.copy(true);
vactrack2.bstrVal = bstrctrack2;
VARIANT vaiccardno;
VariantInit(&vaiccardno);
vaiccardno.vt = VT_BSTR;
char pcardno[20];
_ltoa(iccardno, pcardno, 10);
_bstr_t _bstr_pcardno(pcardno);
BSTR bstrcardno = _bstr_pcardno.copy(true);
vaiccardno.bstrVal = bstrcardno;
VARIANT vast;
VariantInit(&vast);
vast.vt = VT_BSTR;
char pstst[20];
_itoa(ist, pstst, 10);
_bstr_t _bstr_pstst(pstst);
BSTR bstrstst = _bstr_pstst.copy(true);
vast.bstrVal = bstrstst;
VARIANT vaBreakfast;
VariantInit(&vaBreakfast);
vaBreakfast.vt = VT_BSTR;
char pstBreakfast[20];
_itoa(iBreakfast, pstBreakfast, 10);
_bstr_t _bstr_pstBreakfast(pstBreakfast);
BSTR bstrstBreakfast = _bstr_pstBreakfast.copy(true);
vaBreakfast.bstrVal = bstrstBreakfast;
/*
SLOG_INFO("croom: %s", croom);
SLOG_INFO("cgate: %s", cgate);
SLOG_INFO("cstime: %s", cstime);
SLOG_INFO("cguestname: %s", cguestname);
SLOG_INFO("cguestid: %s", cguestid);
SLOG_INFO("clift: %s", clift);
SLOG_INFO("ctrack1: %s", ctrack1);
SLOG_INFO("ctrack2: %s", ctrack2);
SLOG_INFO("iccardno: %d", iccardno);
SLOG_INFO("ist: %d", ist);
SLOG_INFO("iBreakfast: %d", iBreakfast);
*/
delete[] croom; croom = NULL;
delete[] cgate; cgate = NULL;
delete[] cstime; cstime = NULL;
delete[] cguestname; cguestname = NULL;
delete[] cguestid; cguestid = NULL;
delete[] clift; clift = NULL;
delete[] ctrack1; ctrack1 = nullptr;
delete[] ctrack2; ctrack2 = nullptr;
//delete[] iccardno; iccardno = nullptr;
//delete[] ist; ist = nullptr;
//delete[] iBreakfast; iBreakfast = nullptr;
char *pR = new char[4];
_itoa(iR, pR, 10);
_bstr_t _bstr_rr(pR);
BSTR bstrr = _bstr_rr.copy(true);
*vaR = bstrr;
delete[]pR;
pR = nullptr;
return S_OK;
}
//typedef int(*NewKeyF)(char *room, char *gate, char *gate, char *guestname, char *guestid, char *lift, int overflag, int Breakfast, long *cardno, char * track1, char * track2);
STDMETHODIMP CHotelImp::NewKey(VARIANT room, VARIANT gate, VARIANT stime, VARIANT guestname, VARIANT guestid, VARIANT lift, VARIANT overflag, VARIANT breakfast, VARIANT* cardno, VARIANT track1, VARIANT track2, BSTR* vaR)
{
// TODO: 在此添加实现代码
if (hDll == nullptr)
{
return -100000; //dll加载失败
}
_bstr_t bstr_room(room.bstrVal);
char *pcroom = bstr_room;
_bstr_t bstr_gate(gate.bstrVal);
char *pcgate = bstr_gate;
_bstr_t bstr_stime(stime.bstrVal);
char *pcstime = bstr_stime;
_bstr_t bstr_guestname(guestname.bstrVal);
char *pcguestname = bstr_guestname;
_bstr_t bstr_guestid(guestid.bstrVal);
char *pcguestid = bstr_guestid;
_bstr_t bstr_lift(lift.bstrVal);
char *pclift = bstr_lift;
long loverflag = overflag.lVal;
long lbreakfast = breakfast.lVal;
_bstr_t bstr_track1(track1.bstrVal);
char *pctrack1 = bstr_track1;
_bstr_t bstr_track2(track2.bstrVal);
char *pctrack2 = bstr_track2;
int iR = -1111;
long lcardno = 0;
NewKeyF NewKeyFun = (NewKeyF)GetProcAddress(hDll, "NewKey"); //"NewKey"是DLL里的函数名
if (NewKeyFun != NULL)
{
iR = NewKeyFun(pcroom, pcgate, pcstime, pcguestname, pcguestid, pclift, loverflag, lbreakfast, &lcardno, pctrack1, pctrack2);
}
VARIANT vaiccardno;
VariantInit(&vaiccardno);
vaiccardno.vt = VT_BSTR;
char pcardno[20];
_ltoa(lcardno, pcardno, 10);
_bstr_t _bstr_pcardno(pcardno);
BSTR bstrcardno = _bstr_pcardno.copy(true);
vaiccardno.bstrVal = bstrcardno;
char *pR = new char[4];
_itoa(iR, pR, 10);
_bstr_t _bstr_rq(pR);
BSTR bstrq = _bstr_rq.copy(true);
*vaR = bstrq;
delete[]pR;
pR = nullptr;
return S_OK;
}
// HotelImp.h : CHotelImp 的声明
#pragma once
#include "resource.h" // 主符号
#include "Hotel_i.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;
// CHotelImp
class ATL_NO_VTABLE CHotelImp :
public CComObjectRootEx<CComSingleThreadModel>,
public CComCoClass<CHotelImp, &CLSID_HotelImp>,
public IDispatchImpl<IHotelImp, &IID_IHotelImp, &LIBID_HotelLib, /*wMajor =*/ 1, /*wMinor =*/ 0>
{
public:
CHotelImp()
{
hDll = LoadLibrary(_T("MainDll.dll")); //加载dll
}
DECLARE_REGISTRY_RESOURCEID(IDR_HOTELIMP)
BEGIN_COM_MAP(CHotelImp)
COM_INTERFACE_ENTRY(IHotelImp)
COM_INTERFACE_ENTRY(IDispatch)
END_COM_MAP()
DECLARE_PROTECT_FINAL_CONSTRUCT()
HRESULT FinalConstruct()
{
return S_OK;
}
void FinalRelease()
{
FreeLibrary(hDll); // 卸载dll
}
public:
STDMETHOD(Init)(VARIANT software, VARIANT server, VARIANT username, VARIANT port, VARIANT encoder, VARIANT TMEncoder, BSTR* vaR);
STDMETHOD(ReadCard)(VARIANT * room, VARIANT * gate, VARIANT * stime, VARIANT * guestname, VARIANT * guestid, VARIANT * lift, VARIANT * track1, VARIANT * track2, VARIANT * cardno, VARIANT * st, VARIANT * breakfast, BSTR* vaR);
STDMETHOD(NewKey)(VARIANT room, VARIANT gate, VARIANT stime, VARIANT guestname, VARIANT guestid, VARIANT lift, VARIANT overflag, VARIANT breakfast, VARIANT * cardno, VARIANT track1, VARIANT track2, BSTR* vaR);
private:
HINSTANCE hDll; //DLL句柄
};
OBJECT_ENTRY_AUTO(__uuidof(HotelImp), CHotelImp)
// Hotel.idl : Hotel的 IDL 源
//
// 此文件将由 MIDL 工具处理以
// 产生类型库(Hotel.tlb)和封送处理代码。
import "oaidl.idl";
import "ocidl.idl";
[
object,
uuid(10A8CD78-D8B1-4F21-91B5-1120D77D6589),
dual,
nonextensible,
pointer_default(unique)
]
interface IHotelImp : IDispatch{
[id(1)] HRESULT Init([in] VARIANT software, [in] VARIANT server, [in] VARIANT username, [in] VARIANT port, [in] VARIANT encoder, [in] VARIANT TMEncoder, [out, retval] BSTR* vaR);
[id(2)] HRESULT ReadCard([out] VARIANT* room, [out] VARIANT* gate, [out] VARIANT* stime, [out] VARIANT* guestname, [out] VARIANT* guestid, [out] VARIANT* lift, [out] VARIANT* track1, [out] VARIANT* track2, [out] VARIANT* cardno, [out] VARIANT* st, [out] VARIANT* breakfast, [out, retval] BSTR* vaR);
[id(3)] HRESULT NewKey([in] VARIANT room, [in] VARIANT gate, [in] VARIANT stime, [in] VARIANT guestname, [in] VARIANT guestid, [in] VARIANT lift, [in] VARIANT overflag, [in] VARIANT breakfast, [out] VARIANT* cardno, [in] VARIANT track1, [in] VARIANT track2, [out, retval] BSTR* vaR);
};
[
uuid(B741D6CF-105E-4D8A-BFD4-02E7BEECCBE0),
version(1.0),
]
library HotelLib
{
importlib("stdole2.tlb");
[
uuid(C1196FFA-4364-4E08-AA43-A01D12DE86D0)
]
coclass HotelImp
{
[default] interface IHotelImp;
};
};