日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > C# >内容正文

C#

关于C#调用API的理解(汇多考勤机HD4K)

發(fā)布時間:2025/3/21 C# 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 关于C#调用API的理解(汇多考勤机HD4K) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

很多時候為了做集成我們別無選擇的要用C#來調(diào)用C/C++的API,如果你對C#和C/C++數(shù)據(jù)類型對應(yīng)關(guān)系不是很了解,那么這就是一件很頭疼的事情了。

下面來看看具體怎么使用

?

void*?OpenDevice(int?nMyAddress,?HWND?hWnd)
void?CloseDevice(void*?pDevice)
void?SetMyAddress(void*?pDevice,?int?nAddress)?
BOOL?ConnectPort(
void*?pDevice,?LPCTSTR?lpCommDef)
BOOL?IsConnect(
void*?pDevice)
void?SetAddLog(void*?pDevice,?BOOL?bAddLog)
void?SetCommKey(void*?pDevice,?LPCTSTR?lpCommKey)
void?SetInnerCode?(void*?pDevice,?BOOL?bBig5)
BOOL?DisConnectPort(
void*?pDevice)
void?SetWaitTime(void*?pDevice,?DWORD?dwWaitTime)
void*?StartICDMCommand(void*?pDevice,?int?nAddress,?int?nCommand,?
void*?pParameters?=?NULL,?int?nSizeOfParameter?=?0)
int?GetSizeOfData(void*?pCommand)
BOOL?GetData(
void*?pCommand,?void*?pDataBuffer,?int?nSize)
int?GetCmdResult(void*?pCommand)
void?EndICDMCommand(void*?pCommand)
void?RecvEdition(LPTSTR?lpEdition)

上面是api接口原函數(shù)

如果想要在C#里面使用就要重新定義以為他是非托管類行

?

[DllImport("DLL4k.dll",?ExactSpelling?=?false,?EntryPoint?=?"OpenDevice")]
public?static?extern?IntPtr?OpenDevice(int?nMyAddress,?IntPtr?hwnd);
[DllImport(
"DLL4k.dll",?SetLastError?=?true,?EntryPoint?=?"CloseDevice")]
public?static?extern?void?CloseDevice(IntPtr?hwnd);
[DllImport(
"DLL4k.dll",?SetLastError?=?true)]
public?static?extern?void?SetMyAddress(IntPtr?hwnd,?int?nMyAddress);
[DllImport(
"DLL4k.dll",?ExactSpelling?=?false,?EntryPoint?=?"ConnectPort")]
public?static?extern?bool?ConnectPort(IntPtr?pDevice,?StringBuilder?port);??
[DllImport(
"DLL4k.dll",?SetLastError?=?true,?EntryPoint?=?"IsConnect")]
[
return:?System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
public?static?extern?bool?IsConnect(IntPtr?pDevice);
[DllImport(
"DLL4k.dll",?SetLastError?=?true,?EntryPoint?=?"SetAddLog")]
public?static?extern?void?SetAddLog(IntPtr?pDevice,?bool?isaddlog);
[DllImport(
"DLL4k.dll",?SetLastError?=?true)]
public?static?extern?void?SetCommKey(IntPtr?pDevice,?string?str);
[DllImport(
"DLL4k.dll",?SetLastError?=?true)]
public?static?extern?void?SetInnerCode(IntPtr?pDevice,?bool?isbog5);
[DllImport(
"DLL4k.dll",?SetLastError?=?true,?EntryPoint?=?"DisConnectPort")]
[
return:?System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
public?static?extern?bool?DisConnectPort(IntPtr?pDevice);
[DllImport(
"DLL4k.dll",?SetLastError?=?true,?EntryPoint?=?"SetWaitTime")]
public?static?extern?void?SetWaitTime(IntPtr?pDevice,?int?dwWaitTime);
[DllImport(
"DLL4k.dll",?ExactSpelling?=?false,?EntryPoint?=?"StartICDMCommand")]
public?static?extern?IntPtr?StartICDMCommand(IntPtr?pDevice,?int?nAddress,?int?nCommand,?IntPtr?pParameters,?int?nSizeOfParameter?=?0);
[DllImport(
"DLL4k.dll",?EntryPoint?=?"GetSizeOfData",?SetLastError?=?true,?CharSet?=?CharSet.Auto)]
public?static?extern?int?GetSizeOfData(IntPtr?pParameters);
[DllImport(
"DLL4k.dll",?EntryPoint?=?"GetData",?SetLastError?=?true)]
[
return:?System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
public?static?extern?bool?GetData(IntPtr?pCommand,?IntPtr?pDataBuffer,?int?nSize);
[DllImport(
"DLL4k.dll",?SetLastError?=?true,?EntryPoint?=?"GetCmdResult")]
public?static?extern?int?GetCmdResult(IntPtr?pCommand);
[DllImport(
"DLL4k.dll",?SetLastError?=?true,?EntryPoint?=?"EndICDMCommand")]
public?static?extern?void?EndICDMCommand(IntPtr?pDevice);
[DllImport(
"DLL4k.dll",?SetLastError?=?true)]
public?static?extern?void?RecvEdition(string?str);

?

以上就是c#調(diào)用函數(shù)一定要是靜態(tài)的

下面來看看一個列子

?

public?IntPtr?hwnd;
StringBuilder?sb?
=?new?StringBuilder();
public?static?IntPtr?showhwnd?=?System.Diagnostics.Process.GetCurrentProcess().Handle;
public?static?IntPtr?api?=?OpenDevice(0,?showhwnd);
public?IntPtr?kk?=?new?IntPtr();
public?static?int?rcount?=?0;

以上要是全局變量

最后是下載數(shù)據(jù)的調(diào)用例子

sb.Clear();
sb.AppendFormat(
"{0}:baud={1},parity=N,data=8,stop=1",?cbPort.Text,?cbRate.Text);
int?count?=?0;
if?(!IsConnect(api))
{
????
bool?islink?=?ConnectPort(api,?sb);
????DMSTATUS?dmm?
=?new?DMSTATUS();
????
int?sizenewb?=?Marshal.SizeOf(dmm);
? ? IntPtr?buffernew?
=?Marshal.AllocCoTaskMem(sizenewb);
????
int?sizenewa?=?0;
????
try
? ? {
????
//將托管對象拷貝到非托管內(nèi)存
? ? SetWaitTime(api,?3000);
? ? Marshal.StructureToPtr(dmm,?buffernew,?
true);
? ? SetAddLog(api,?
true);
? ? ? ? kk?
=?StartICDMCommand(api,?0,?(int)CMDWORD.nRecvDMStatus,?buffernew,?sizenewb);
? ? ? ? rcount?
=?GetCmdResult(kk);
????????
if?(rcount?!=?0)
? ? ? ? {
? ? ? ? ? ??sizenewa?
=?GetSizeOfData(kk);
????????????
bool?isok?=?GetData(kk,?buffernew,?sizenewa);
????????????DMSTATUS?mm?
=?(DMSTATUS)Marshal.PtrToStructure(buffernew,?typeof(DMSTATUS));
????????????count?
=?mm.nRecordCount;
????????????EndICDMCommand(kk);
????????????StringBuilder?sbt?
=?new?StringBuilder();
????????????
int?totalcount?=?0;
????????????
if?(count?%?100?==?0)
????????????{
? ? ? ? ? ? totalcount?
=?count?/?100;
????????????}
????????????
else
????????????{
? ? ? ? ? ? ? ?totalcount?
=?(count?/?100)?+?1;
????????????}
????????????
for?(int?i?=?0;?i?<?totalcount;?i++)
????????????{
???????????????
bool?link?=?ConnectPort(api,?sb);
???????????????
if?(link)
? ? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? ? ATTGUARDRECORD[]?Record?
=?new?ATTGUARDRECORD[100];
??????????????????
int?sizerecord?=?Marshal.SizeOf(Record[0]);
? ? ? ? ? ? ? ? ? IntPtr?bufferrecord?
=?Marshal.AllocCoTaskMem(sizerecord?*?100);
? ? ? ? ? ? ? ? ? kk?
=?StartICDMCommand(api,?0,?(int)CMDWORD.nRecvDelRecord,?bufferrecord,?sizerecord);
? ? ? ? ? ? ? ? ? rcount?
=?GetCmdResult(kk);
??????????????????
if?(rcount?>?0)
? ? ? ? ? ? ? ? ? {
?????????????????????
int?sizecount?=?GetSizeOfData(kk);
? ? ? ? ? ? ? ? ? ? ?GetData(kk,?bufferrecord,?sizecount);
?????????????????????
for?(int?j?=?0;?j?<?100;?j++)
? ? ? ? ? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? ? ? ?Record[j]?
=?(ATTGUARDRECORD)Marshal.PtrToStructure((IntPtr)(bufferrecord?+?sizerecord?*?j),?typeof(ATTGUARDRECORD));
?????????????????????
if?(Record[j].nYear?>?0)
? ? ? ? ? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? ? ? ? ?sbt.AppendLine(Record[j].strPersonID?
+?"-"?+Record[j].nYear.ToString()?+?"-"?+?Record[j].nMonth.ToString()?+?"-"?+?Record[j].nDay.ToString()?+?"-"+?Record[j].nHour.ToString()?+?":"?+?Record[j].nMinute.ToString()+?":"?+?Record[j].nSecond.ToString()+?"-"?+Record[j].bOnDuty.ToString()?+?"-"?+?Record[j].bBC.ToString()?+?"-"?+?Record[j].nDMAdr);
????????????????????????????????}
????????????????????????????????
else
????????????????????????????????{
????????????????????????????????????
continue;
????????????????????????????????}
????????????????????????}
????????????????????????IntPtr?del?
=?new?IntPtr();
????????????????????????StartICDMCommand(api,?
0,?(int)CMDWORD.nRecvDelRecordRlt,?del);
????????????????????????}
????????????????????}
????????????????????EndICDMCommand(kk);
????????????}
????????????txtcontent.Text?
=?sbt.ToString();
????????????}
????????}
????????
catch
????????{

????????}

}?

這樣就結(jié)束了

轉(zhuǎn)載于:https://www.cnblogs.com/keepsilence/archive/2011/09/20/2182373.html

總結(jié)

以上是生活随笔為你收集整理的关于C#调用API的理解(汇多考勤机HD4K)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。