C++\CLI语法 在项目中的使用
通常情况下,对一个标准的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语法 在项目中的使用的更多相关文章
- 项目中lua(基础)
关于项目中lua任务(某些没弄懂,但lua上耗费时间有点长了不看了) 这段时间看了lua语法和项目中lua应用 .在lua中注册c库,在lua5.2中好像都是注册c库,相当于在lua中定义一个tabl ...
- 项目中oracle存储过程记录——经常使用语法备忘
项目中oracle存储过程记录--经常使用语法备忘 项目中须要写一个oracle存储过程,需求是收集一个复杂查询的内容(涉及到多张表),然后把符合条件的记录插入到目标表中.当中原表之中的一个的日期字段 ...
- scss/less语法以及在vue项目中的使用(转载)
1.scss与less都是css的预处理器,首先我们的明白为什么要用scss与less,因为css只是一种标记语言,其中并没有函数变量之类的,所以当写复杂的样式时必然存在局限性,不灵活,而scss与l ...
- vue之cli脚手架项目中组件的使用
在webpack-simple模板中,包括webpck模板.一个.vue文件就是一个组件. 为什么会这样呢?因为webpack干活了!webpack的将我们所有的资源文件进行打包.同时webpack还 ...
- nuxt 脚手架创建nuxt项目中不支持es6语法的解决方案
node本身并不支持es6语法,我们通常在vue项目中使用es6语法,是因为,我们使用babel做过处理, 为了让项目支持es6语法,我们必须同时使用babel 去启动我们的程序,所以再启动程序中加 ...
- 关于CUDA C 项目中“ error C2059: 语法错误:“<” ”问题的解决方法
该问题的关键在于理解CUDA项目中C\C++文件需要由c++编译器进行编译,而CUDA C的源文件需要由CUDA的编译器nvcc.exe进行编译. 发生该语法错误的原因是cu文件被C++编译器所编译, ...
- Vue 项目中的ESlint语法报错问题
在项目中的""和;经常会报错,真的很纠结,今天看到一个解决方法,可以不用卸载删除 在项目根目录中,新建一个.prettierrc文件,来移除分号,和替换为单引号. { " ...
- Vue项目中sass语法该怎么用?
最近开始着手Vue框架,被各种报错蹂躏,其中有一个就是sass语法,<style>标签中添加<style lang="scss">,发现报错,在网上找了一些 ...
- 解决Vite-React项目中js使用jsx语法报错的问题
背景 在做存量项目接入Vite测试时发现,存量(老)项目中很多是直接在js中书写jsx语法,使用Vite启动时就会抛出一堆问题Failed to parse source. 不嫌麻烦可以跑个脚本批量修 ...
随机推荐
- git branch --set-upstream 本地关联远程分支
最近使用git pull的时候多次碰见下面的情况: There is no tracking information for the current branch.Please specify whi ...
- kettle查询-2
模糊匹配: 1.主数据/查询数据 2.模糊匹配 3.输出:jaro/jaro winkler/pair letters similarity(各自算法的匹配度measure value) http c ...
- AirBnB春招笔试题
试题说明 笔试题只有一道,限时1小时. 模拟一个战争外交游戏,游戏中定义了三种操作: A city1 Hold : 军队A 占领了city1 A city1 Move city2 : 军队A从city ...
- 用UE4蓝图制作FPS_零基础学虚幻4第二季
课时1:案例演示 05:12 课时2:工程准备 07:35 (把一个项目从一个工程移动到另一个工程) 1.新建一个空白工程,不包含初学者内容 2.选择我们要复制的工程,按右键,如下图: 复制到新工程的 ...
- 【系统架构】软件核心复杂性应对之道-领域驱动DDD(Domain-Driven Design)
前言 领域驱动设计是一个开放的设计方法体系,目的是对软件所涉及到的领域进行建模,以应对系统规模过大时引起的软件复杂性的问题,本文将介绍领域驱动的相关概念. 一.软件复杂度的根源 1.业务复杂度(软件的 ...
- 20165223 《信息安全系统设计基础》 实现mypwd
一.学习pwd命令 1. pwd命令简介 英文原名:Print Working Directory 指令功能:打印出当前工作目录 执行权限:All User 指令所在路径:/usr/bin/pwd 或 ...
- maven 使用 log4j
Log4j是Apache的一个开源项目,通过使用Log4j,我们可以控制日志信息输送的目的地是控制台.文件.GUI组件,甚至是套接口服务器.NT的事件记录器.UNIX Syslog守护进程等:我们也可 ...
- gprof性能测试工具
1.编译时加-pg选项,例如:gcc -pg test.c-o test_gprof.其中test后的_gprof一定要加上.会生成gmon.out. 2.运行程序.gprof test_gprof ...
- JS控制开灯关灯
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- MySQL5.7.23解压版安装教程
每次找安装教程太麻烦,因此给自己备份一下步骤,方便以后查看.解压版下载地址https://dev.mysql.com/downloads/mysql/,详细图解如下: 1.根据自己需求,选择适合自己的 ...