求 C++ 转 C#
以下是C++写的一个demo,我不懂C++,需要高人帮我翻译一下,如何转成C#
#include <Windows.h>
#include "libMPRDevice/win/MPRDeviceAPI.h"
#include <assert.h>
#include <stdio.h>
#include <tchar.h>
#include <time.h>
static int check_checksum(const unsigned char digit[16])
{
int sum = 0, check = 0, x;
for (x = 0; x < 15; x++)
{
if (0 == x % 2)
check = (digit[x] - '0') * 1;
else
check = (digit[x] - '0') * 2;
sum += (check / 10 + check % 10);
}
check = 10 - (sum % 10);
if (10 == check)
{
check = 0;
}
return (check == (digit[15] - '0'));
}
int mpr_bytecode_to_digicode(const unsigned char mpr_byte_code[7],
unsigned char prefix_code[10],
unsigned char suffix_code[5])
{
unsigned char digit_code[16];
unsigned long long v = 0;
unsigned long long t = 0;
int i = 0;
t = mpr_byte_code[0]; t <<= 48; v |= t;
t = mpr_byte_code[1]; t <<= 40; v |= t;
t = mpr_byte_code[2]; t <<= 32; v |= t;
t = mpr_byte_code[3]; t <<= 24; v |= t;
t = mpr_byte_code[4]; t <<= 16; v |= t;
t = mpr_byte_code[5]; t <<= 8; v |= t;
t = mpr_byte_code[6]; t <<= 0; v |= t;
if (0 == v)
{
return 0;
}
for (i = 15; i >=0; i--)
{
digit_code[i] = (v % 10) + '0';
v /= 10;
}
if (check_checksum(digit_code))
{
memcpy(prefix_code, digit_code, 10);
memcpy(suffix_code, digit_code + 10, 5);
return 1;
}
else
{
return 0;
}
}
void ProcessMPRDeviceEvent(MPRDeviceEvent event, //[IN] 事件类型
const unsigned char* p_data, //[IN] 描述事件详细信息的数据的首地址
int data_len) //[IN] 描述事件详细信息的数据长度
{
char info[256];
switch (event)
{
case PLUGED_IN:
sprintf_s(info, _T(".......................%s: PLUGED_IN...........................\n"), p_data);
OutputDebugString(info);
//MPR设备接入,p_data指向设备的序列号
//TODO:用异步消息通知主线程设备接入
break;
case PLUGED_OUT:
sprintf_s(info, _T(".......................%s: PLUGED_OUT...........................\n"), p_data);
OutputDebugString(info);
//MPR设备拔出,p_data指向设备的序列号
//TODO:用异步消息通知主线程设备拔出
break;
case MPR_CODE_CAPTURED:
{
sprintf_s(info,_T("MPR CODE: %.2x %.2x %.2x %.2x %.2x %.2x %.2x \n"),\
p_data[0],p_data[1],p_data[2],p_data[3],p_data[4],p_data[5],p_data[6]);
OutputDebugString(info);
printf(info);
unsigned char prefix_code[11] = {0};
unsigned char suffix_code[6] = {0};
mpr_bytecode_to_digicode(p_data, prefix_code, suffix_code);
prefix_code[10] = 0;
suffix_code[5] = 0;
info[0] = 0;
sprintf_s(info, _T("前置码:%s 后置码:%s\n\n"), prefix_code, suffix_code);
OutputDebugString(info);
printf(info);
//点读了一个MPR码,码值为p_data为首的7个字节
//TODO:用异步消息通知主线程点读了一个MPR码
break;
}
case POWER_KEY_PRESSED:
sprintf_s(info,_T(".......................POWER_KEY_PRESSED....type:%x.......................\n"),\
p_data[0]);
OutputDebugString(info);
//用户按下了电源键,p_data[0]为0表示短按,否则表示长按
//TODO:用异步消息通知主线程按键事件发生
break;
case UP_KEY_PRESSED:
sprintf_s(info,_T(".......................UP_KEY_PRESSED....type:%x.......................\n"),\
p_data[0]);
OutputDebugString(info);
//用户按下了"+"键,p_data[0]为0表示短按,否则表示长按
//TODO:用异步消息通知主线程按键事件发生
break;
case DOWN_KEY_PRESSED:
sprintf_s(info,_T(".......................DOWN_KEY_PRESSED....type:%x.......................\n"),\
p_data[0]);
OutputDebugString(info);
//用户按下了"-"键,p_data[0]为0表示短按,否则表示长按
//TODO:用异步消息通知主线程按键事件发生
break;
default:
//trace("MPRDeviceAPI: unsupported mpr device event!");
break;
}
}
extern "C"
{
typedef int (*tp_MPRDevice_InitLib)(
FP_MPRDeviceEventHandleProc fp_callback //[IN] 注入事件处理回调函数
);
typedef int (*tp_MPRDevice_EnumConnectedDevice)(
char dev_sn_list[1024] //[OUT] 返回换行符'\n'分割的设备序列号
);
typedef int (*tp_MPRDevice_CaptureEventFocus)(
const char* p_dev_sn //[IN] 设备序列号
);
typedef int (*tp_MPRDevice_StartCaptureMPRCode)(
const char* p_dev_sn //[IN] 设备序列号
);
}
tp_MPRDevice_InitLib p_MPRDevice_InitLib = NULL;
tp_MPRDevice_EnumConnectedDevice p_MPRDevice_EnumConnectedDevice = NULL;
tp_MPRDevice_CaptureEventFocus p_MPRDevice_CaptureEventFocus = NULL;
tp_MPRDevice_StartCaptureMPRCode p_MPRDevice_StartCaptureMPRCode = NULL;
int main()
{
HMODULE hMoudule = LoadLibrary(_T("MPRDeviceAPI.dll"));
assert(hMoudule != NULL);
p_MPRDevice_InitLib = (tp_MPRDevice_InitLib)GetProcAddress(hMoudule, _T("MPRDevice_InitLib"));
assert(p_MPRDevice_InitLib != NULL);
p_MPRDevice_EnumConnectedDevice = (tp_MPRDevice_EnumConnectedDevice)GetProcAddress(hMoudule, _T("MPRDevice_EnumConnectedDevice"));
assert(p_MPRDevice_EnumConnectedDevice != NULL);
p_MPRDevice_CaptureEventFocus = (tp_MPRDevice_CaptureEventFocus)GetProcAddress(hMoudule, _T("MPRDevice_CaptureEventFocus"));
assert(p_MPRDevice_CaptureEventFocus != NULL);
p_MPRDevice_StartCaptureMPRCode = (tp_MPRDevice_StartCaptureMPRCode)GetProcAddress(hMoudule, _T("MPRDevice_StartCaptureMPRCode"));
assert(p_MPRDevice_StartCaptureMPRCode != NULL);
p_MPRDevice_InitLib( ProcessMPRDeviceEvent );
char sn[1024];
memset(sn, 0, sizeof(sn));
p_MPRDevice_EnumConnectedDevice(sn);
p_MPRDevice_CaptureEventFocus(sn);
int rt = p_MPRDevice_StartCaptureMPRCode(sn);
Sleep(INFINITE );
}