通常情况下,对一个标准的com组件进行集成,网上普遍使用的方式有:

1、#import *.dll 或 #import *.ocx的方式,VS编译器重新编译后,就会自动生成组件对应的*.tlh文件,该文件类似于头文件,含有com组件中的接口信息,在自己的项目中就可以引用接口或者类了。
2、#pragma comment(lib, "*.lib")的方式,这种方式可以直接引用lib里的接口声明。

好巧不巧:项目中碰到的三方组件恰好用这两种方式都不能集成:用第一种方式,VS生成的tlh文件内容是空的;第二种直接引用后,编译报错

通过三方组件给的C#调用的示例程序,发现该组件的目标运行平台必须选定是X86平台

自身项目属于C++范畴,所以在查阅部分资料后,遇到了C++/CLI

#include <Windows.h>

#include <stdio.h>

// 在做类型转换时用到
using namespace System; // 组件的路径放在了本地,在实际运行中,将该组件放在了和运行程序的同目录下
#using "E:\C_backup_20180315\jzbank\CallComSln\CallCom\Debug\Interop.SmartCardLib.dll" // SmartCardLib是Interop.SmartCardLib.dll内的命名空间
using namespace SmartCardLib; // 组件的路径放在了本地,在实际运行中,将该组件放在了和运行程序的同目录下
#using "E:\C_backup_20180315\jzbank\CallComSln\CallCom\Debug\Interop.IObjectSafetyTLB.dll" // IObjectSafetyTLB是Interop.IObjectSafetyTLB.dll内的命名空间
using namespace IObjectSafetyTLB; // 时间工具
static SYSTEMTIME t; void main(){ // clsSmartCardClass是namespace SmartCardLib里的
// cli语法 注意 ^ 和 gcnew
clsSmartCardClass ^instance = gcnew clsSmartCardClass(); int ret = -; // namespace SmartCardLib里声明的方法
ret = instance->Connect("param1"); Sleep(); // cli语法 声明变量并初始化
System::Object^ cardNo = gcnew System::Object();
System::Object^ customNo1 = gcnew System::Object();
System::Object^ orderdate1 = gcnew System::Object();
System::Object^ orderAmount1 = gcnew System::Object(); System::Object^ orderNum1 = gcnew System::Object();
System::Object^ orderTotal1 = gcnew System::Object();
System::Object^ fgs1 = gcnew System::Object();
System::Object^ glz1 = gcnew System::Object(); System::Object^ cardStatus1 = gcnew System::Object();
System::Object^ cardPwd1 = gcnew System::Object();
System::Object^ cardType1 = gcnew System::Object();
System::Object^ chgPwd1 = gcnew System::Object(); // Int32
int result = instance->readCard(cardNo, customNo1, orderdate1, orderAmount1,
//
orderNum1, orderTotal1, fgs1, glz1,
//
cardStatus1, cardPwd1, cardType1, chgPwd1); // 文件生成在本地,临时写在特定目录
FILE *pFile;
fopen_s( &pFile, "C:\\Users\\Administrator\\teller\\device\\1\\infofile\\info.txt", "w+" ); if (result == )
{
char resv[] = {}; //////////////////////////////////////////////
// cli语法 将 System::String^ 转换为 char*
strcpy(resv, (char*)(void*)System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi((System::String^)cardNo));
printf("\nok, instance->readCard()-----------cardNo=%s", resv); GetLocalTime(&t);
fprintf(pFile, "%4d/%02d/%02d %02d:%02d:%02d.%03d %s\n",
t.wYear,t.wMonth,t.wDay,t.wHour,t.wMinute, t.wSecond,t.wMilliseconds, resv);
fflush(pFile); memset(resv, , sizeof(resv)); //////////////////////////////////////////////
strcpy(resv, (char*)(void*)System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi((System::String^)customNo1));
printf("\nok, instance->readCard()-----------customNo1=%s", resv); GetLocalTime(&t);
fprintf(pFile, "%4d/%02d/%02d %02d:%02d:%02d.%03d %s\n",
t.wYear,t.wMonth,t.wDay,t.wHour,t.wMinute, t.wSecond,t.wMilliseconds, resv);
fflush(pFile); memset(resv, , sizeof(resv)); //////////////////////////////////////////////
strcpy(resv, (char*)(void*)System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi((System::String^)orderdate1));
printf("\nok, instance->readCard()-----------orderdate1=%s", resv); GetLocalTime(&t);
fprintf(pFile, "%4d/%02d/%02d %02d:%02d:%02d.%03d %s\n",
t.wYear,t.wMonth,t.wDay,t.wHour,t.wMinute, t.wSecond,t.wMilliseconds, resv);
fflush(pFile); memset(resv, , sizeof(resv)); //////////////////////////////////////////////
GetLocalTime(&t);
fprintf(pFile, "%4d/%02d/%02d %02d:%02d:%02d.%03d %d\n",
t.wYear,t.wMonth,t.wDay,t.wHour,t.wMinute, t.wSecond,t.wMilliseconds, (int)(System::Int32)orderAmount1);
fflush(pFile);
printf("\nok, instance->readCard()-----------orderAmount1=%d", (int)(System::Int32)orderAmount1); //////////////////////////////////////////////
GetLocalTime(&t);
fprintf(pFile, "%4d/%02d/%02d %02d:%02d:%02d.%03d %d\n",
t.wYear,t.wMonth,t.wDay,t.wHour,t.wMinute, t.wSecond,t.wMilliseconds, (int)(System::Int16)orderNum1);
fflush(pFile);
printf("\nok, instance->readCard()-----------orderNum1=%d", (int)(System::Int16)orderNum1); //////////////////////////////////////////////
GetLocalTime(&t);
fprintf(pFile, "%4d/%02d/%02d %02d:%02d:%02d.%03d %d\n",
t.wYear,t.wMonth,t.wDay,t.wHour,t.wMinute, t.wSecond,t.wMilliseconds, (int)(System::Int32)orderTotal1);
fflush(pFile);
printf("\nok, instance->readCard()-----------orderTotal1=%d", (int)(System::Int32)orderTotal1); //////////////////////////////////////////////
strcpy(resv, (char*)(void*)System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi((System::String^)fgs1));
printf("\nok, instance->readCard()-----------fgs1=%s", resv); GetLocalTime(&t);
fprintf(pFile, "%4d/%02d/%02d %02d:%02d:%02d.%03d %s\n",
t.wYear,t.wMonth,t.wDay,t.wHour,t.wMinute, t.wSecond,t.wMilliseconds, resv);
fflush(pFile); memset(resv, , sizeof(resv)); //////////////////////////////////////////////
strcpy(resv, (char*)(void*)System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi((System::String^)glz1));
printf("\nok, instance->readCard()-----------glz1=%s", resv); GetLocalTime(&t);
fprintf(pFile, "%4d/%02d/%02d %02d:%02d:%02d.%03d %s\n",
t.wYear,t.wMonth,t.wDay,t.wHour,t.wMinute, t.wSecond,t.wMilliseconds, resv);
fflush(pFile); memset(resv, , sizeof(resv)); //////////////////////////////////////////////
strcpy(resv, (char*)(void*)System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi((System::String^)cardStatus1));
printf("\nok, instance->readCard()-----------cardStatus1=%s", resv); GetLocalTime(&t);
fprintf(pFile, "%4d/%02d/%02d %02d:%02d:%02d.%03d %s\n",
t.wYear,t.wMonth,t.wDay,t.wHour,t.wMinute, t.wSecond,t.wMilliseconds, resv);
fflush(pFile);
}
else
{
GetLocalTime(&t);
fprintf(pFile, "%4d/%02d/%02d %02d:%02d:%02d.%03d call readCard faild, ret=%d\n",
t.wYear,t.wMonth,t.wDay,t.wHour,t.wMinute, t.wSecond,t.wMilliseconds, result);
} fflush(pFile);
fclose(pFile);
pFile = NULL; int dret = instance->Disconnect();
//printf("\n--, instance->Disconnect()-----------dret=%d", dret); // 此处延时会导致程序即将结束时报错:Runtime error 217 at 。。。
//Sleep(3000); //getchar();
}

在C++项目中调用如下:

#include "LoadDll.h"

#include <ShlObj.h>

//用于生成文件夹 mkdir
#include <direct.h>
#include <string.h> char exePath[MAX_PATH] = {};
ret = checkExePath(exePath, exeName);
if(ret < ) return ret; char params[] = {};
sprintf(params,
"%s|%d|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s",
funcName, timeout, compcode,
tmpCardNo, tmpcustomNo,
orderdate, orderNum, orderAmount,
orderTotal, tmpCardPwd, alertLowLimit,
overDraft, idleDays, upLimit
); ret = CallExe(exePath, params, timeout); // 读取生成的文件
char infoFilePath[MAX_PATH] = {};
ret = checkExePath(infoFilePath, txtName);
if(ret < ) return ret; FILE *pInfoFile = fopen(infoFilePath, "r");
if(NULL == pInfoFile) return -; char output[] = {};
fgets(output, sizeof(output), pInfoFile); fflush(pInfoFile);
fclose(pInfoFile);
pInfoFile = NULL; // 删除临时生成的文件
//ret = remove(infoFilePath);

其中CallExe函数实现为:

int CallExe(char exePath[], const char* params, int timeout)
{
SHELLEXECUTEINFO exe = {}; exe.cbSize = sizeof(SHELLEXECUTEINFO);
exe.fMask = SEE_MASK_NOCLOSEPROCESS;
exe.hwnd = NULL;
exe.lpVerb = NULL;
exe.lpFile = exePath;
exe.lpParameters = params;
exe.lpDirectory = NULL;
//exe.nShow = SW_NORMAL;
exe.nShow = SW_HIDE;
exe.hInstApp = NULL; BOOL suc = ShellExecuteEx(&exe); DWORD m = WaitForSingleObject(exe.hProcess, timeout * ); return (int)m; }

其中checkExePath函数实现为:

int checkExePath(char exePath[], char* fileSimpleName)
{
char path[MAX_PATH] = {};
if (GetDesktopPath(path)){ int desktopPathLenth = strlen((const char *)path); int i = desktopPathLenth - ;
for(; i > ; i--){
if(path[i] == '\\' || path[i] == '/'){
break;
}
} for(int j = ; j < i; j++){
exePath[j] = path[j];
} sprintf(exePath, "%s%s", exePath, "\\fold1"); // 检查“teller”文件夹是否存在
if (!CheckFolderExist(exePath)){
_mkdir(exePath);
} sprintf(exePath, "%s%s", exePath, "\\fold2"); // 检查“device”文件夹是否存在
if (!CheckFolderExist(exePath)){
_mkdir(exePath);
} sprintf(exePath, "%s%s", exePath, "\\fold3"); // 检查“CNG_READER_JZBANK”文件夹是否存在
if (!CheckFolderExist(exePath)){
_mkdir(exePath);
} // 用户目录/fold1/fold2/fold3/fileSimpleName
sprintf(exePath, "%s\\%s", exePath, fileSimpleName);
// 检查exe是否存在 return ;
} return -;
}

GetDesktopPath的函数实现为:

//获取桌面目录的绝对路径
bool GetDesktopPath(char* desktopPath)
{
if (SHGetSpecialFolderPath(NULL, desktopPath, CSIDL_DESKTOP, )){
return TRUE;
} return FALSE;
}

CheckFolderExist的函数实现为:

//目录是否存在的检查:
bool CheckFolderExist(const char* strPath)
{
WIN32_FIND_DATA wfd;
bool rValue = FALSE;
HANDLE hFind = FindFirstFile(strPath, &wfd);
if ((hFind != INVALID_HANDLE_VALUE) && (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)){
rValue = TRUE;
}
FindClose(hFind);
return rValue;
}

C++\CLI语法 在项目中的使用的更多相关文章

  1. 项目中lua(基础)

    关于项目中lua任务(某些没弄懂,但lua上耗费时间有点长了不看了) 这段时间看了lua语法和项目中lua应用 .在lua中注册c库,在lua5.2中好像都是注册c库,相当于在lua中定义一个tabl ...

  2. 项目中oracle存储过程记录——经常使用语法备忘

    项目中oracle存储过程记录--经常使用语法备忘 项目中须要写一个oracle存储过程,需求是收集一个复杂查询的内容(涉及到多张表),然后把符合条件的记录插入到目标表中.当中原表之中的一个的日期字段 ...

  3. scss/less语法以及在vue项目中的使用(转载)

    1.scss与less都是css的预处理器,首先我们的明白为什么要用scss与less,因为css只是一种标记语言,其中并没有函数变量之类的,所以当写复杂的样式时必然存在局限性,不灵活,而scss与l ...

  4. vue之cli脚手架项目中组件的使用

    在webpack-simple模板中,包括webpck模板.一个.vue文件就是一个组件. 为什么会这样呢?因为webpack干活了!webpack的将我们所有的资源文件进行打包.同时webpack还 ...

  5. nuxt 脚手架创建nuxt项目中不支持es6语法的解决方案

    node本身并不支持es6语法,我们通常在vue项目中使用es6语法,是因为,我们使用babel做过处理, 为了让项目支持es6语法,我们必须同时使用babel 去启动我们的程序,所以再启动程序中加 ...

  6. 关于CUDA C 项目中“ error C2059: 语法错误:“<” ”问题的解决方法

    该问题的关键在于理解CUDA项目中C\C++文件需要由c++编译器进行编译,而CUDA C的源文件需要由CUDA的编译器nvcc.exe进行编译. 发生该语法错误的原因是cu文件被C++编译器所编译, ...

  7. Vue 项目中的ESlint语法报错问题

    在项目中的""和;经常会报错,真的很纠结,今天看到一个解决方法,可以不用卸载删除 在项目根目录中,新建一个.prettierrc文件,来移除分号,和替换为单引号. { " ...

  8. Vue项目中sass语法该怎么用?

    最近开始着手Vue框架,被各种报错蹂躏,其中有一个就是sass语法,<style>标签中添加<style lang="scss">,发现报错,在网上找了一些 ...

  9. 解决Vite-React项目中js使用jsx语法报错的问题

    背景 在做存量项目接入Vite测试时发现,存量(老)项目中很多是直接在js中书写jsx语法,使用Vite启动时就会抛出一堆问题Failed to parse source. 不嫌麻烦可以跑个脚本批量修 ...

随机推荐

  1. 异常SRVE0199E

    后台生成导出exe表格,在tomcat自己环境下完全没问题到websphere环境下保SRVE0199E产生这个问题是因为response.OutputStream已经打开再次打开就报这个异常,前台如 ...

  2. centos7之添加开机启动服务/脚本

    一.添加开机启动脚本 #!/bin/bash # THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES # # It is highly advisable to ...

  3. Python——Django-form表单提交

    一.提交的注意事项 1. form不是from,所有获取用户输入的标签都应该放在form里面, input并且必须要有name属性 2. action属性控制往哪儿提交,method一般都设置成pos ...

  4. git 学习(3) ----- 代码共享和多人协作

    当我们开发项目的时候,项目会越来越大,就有可能需要其它同事进行参与,甚至进行开源,这时就需要找一个地方把代码存放起来,好供其它人下载并开发.这个地方,最好放到服务器上,因为只要能上网,就可以获取到, ...

  5. nginx编译安装指定参数

    --prefix=/app/nginx # 安装目录 --conf-path=/app/nginx/conf/nginx.conf # 配置文件 --sbin-path=/app/nginx/sbin ...

  6. [https]公司导入自签名证书实现https监控

    https://www.v2ex.com/t/143012

  7. Java【第九篇】异常处理

    异常概述 介绍 任何一种程序设计语言设计的程序在运行时都有可能出现错误,例如除数为0,数组下标越界,要读写的文件不存在等等.捕获错误最理想的是在编译期间,但有的错误只有在运行时才会发生.对于这些错误, ...

  8. (转)Java8内存模型—永久代(PermGen)和元空间(Metaspace)

    背景:介绍java8中永久代到元空间的转变. Java8内存模型—永久代(PermGen)和元空间(Metaspace) 一.JVM 内存模型 根据 JVM 规范,JVM 内存共分为虚拟机栈.堆.方法 ...

  9. secureCRT自动断开的解决方法

    转: secureCRT自动断开的解决方法 secureCRT自动断开的解决方法 在secureCRT上登录时,一段时间不用的话会自动断开,必须重新连接,有点麻烦. 有时候服务器端的 /etc/pro ...

  10. cobbler批量安装操作

    打开mirrors.aliyun.com/epel http://mirrors.aliyun.com/epel/epel-release-latest-6.noarch.rpm rpm -ivh h ...