手头有一个PCI通讯板卡。有DLL驱动。测试DEMO原来是VC环境下。现在要改成c#环境。这里面涉及到C#如何调用原来DLL的API函数问题。任务单一:就是把原来的VC环境的demo程序用C#写。下面是VC环境的DEMO程序。 // Test_pcitio.cpp // // Generated by DriverWizard version DriverStudio 2.7.0 (Build 562) // // This console application demonstrates how to open a handle // to a device in your driver, and communicate with the driver // using Read, Write, and DeviceIoControl calls, as appropriate. // // This test program attempts to open the device using the // GUID defined in "..PcitioDeviceinterface.h"
#include <stdlib.h> #include <stdio.h> #include <windows.h>
#include <winioctl.h> #include "..pcitioioctl.h"
#include "..PcitioDeviceinterface.h" // Has class GUID definition
// This function is found in module OpenByIntf.cpp HANDLE OpenByInterface(GUID* pClassGuid, DWORD instance, PDWORD pError);
typedef void VOIDFUNC();
HANDLE h,IOWaiter, hEvent; // Prototypes void Usage(void); void ShowIoctlValues(void);
// TODO: // You can redefine the IOCTL handler prototypes as needed, adding // appropriate parameters that can be collected from the command line. // To do this you must modify the command line parsing logic. An // example of this is shown in comments throughout the test application. // //=== Parameterized IOCTL Example === // void Test_IOCTL_PARAMETERIZED(int nVal, ULONG dwVal); //void Test_PCITIO_IOCTL_DMAHEADADD(ULONG mode); //ULONG Test_PCITIO_IOCTL_INTCSR(ULONG seldelay,ULONG selstart,ULONG selend,ULONG selindex,ULONG done); ULONG Test_PCITIO_IOCTL_DMABYTES(ULONG add,ULONG data); void Test_PCITIO_IOCTL_DMAREAD(void); void Test_PCITIO_IOCTL_DMAWRITE(void); /*ULONG startout(ULONG delaynum,ULONG startaddress, ULONG endaddress,ULONG indexaddress ,ULONG channelnum); */ //void writesize(ULONG x,ULONG y); //void writebuf(ULONG address,BYTE data);
void CloseIfOpen(void);
// Global data void ReadOne(); void WriteOne(); void Read(); void Write();
long i = 0; int tp =0; ULONG *wrbuf; BYTE *wrrbuf; BYTE *ttfbuf; ULONG *wrfilebuf; BYTE *membuf; long start=0; long kk=0; ULONG chang,kuan;
//全局缓冲区,就是你们应用程序的内存缓冲区 //现申请大小512M字节,可以修改大小
#define N_IOCODES 6
// Names of IOCTL codes // char *IOnames[N_IOCODES+1] = {
//=== Parameterized IOCTL Example === // "IOCTL_PARAMETERIZED", "PCITIO_IOCTL_DMAHEADADD", "PCITIO_IOCTL_INTCSR", "PCITIO_IOCTL_DMABYTES", "PCITIO_IOCTL_SETEVENT", "PCITIO_IOCTL_DMAREAD", "PCITIO_IOCTL_DMAWRITE", "" };
// IOCTL codes // int IOcodes[N_IOCODES+1] = {
//=== Parameterized IOCTL Example === // IOCTL_PARAMETERIZED, PCITIO_IOCTL_DMAHEADADD, PCITIO_IOCTL_INTCSR, PCITIO_IOCTL_DMABYTES, PCITIO_IOCTL_SETEVENT, PCITIO_IOCTL_DMAREAD, PCITIO_IOCTL_DMAWRITE, 0 };
// Handle to device opened in driver. // HANDLE hDevice = INVALID_HANDLE_VALUE;
// Class GUID used to open device // GUID ClassGuid = PcitioDevice_CLASS_GUID;
//dll function declare start typedef BOOL (/*_stdcall*/ *TDMAHEADADD)(ULONG mode);//void Test_PCITIO_IOCTL_DMAHEADADD(ULONG mode) TDMAHEADADD DmaAdd;
typedef ULONG (*TDMABYTES)(ULONG add,ULONG data);//ULONG Test_PCITIO_IOCTL_DMABYTES(ULONG add,ULONG data) TDMABYTES DmaBytes;
typedef ULONG (*TIOCTLINTCSR)(ULONG seldelay,ULONG selstart,ULONG selend,ULONG selindex,ULONG done); TIOCTLINTCSR TestPciIntCSR;
typedef ULONG (*PStartout)(ULONG delaynum,ULONG startaddress, ULONG endaddress,ULONG indexaddress ,ULONG channelnum); PStartout startout1;
typedef void (*PWriteSize)(ULONG x,ULONG y); PWriteSize writesize;
typedef ULONG (*PWriteBuf)(ULONG address,BYTE data); PWriteBuf writebuf;
typedef ULONG (*PTransData)(ULONG channelnum); PTransData Transdata;
///dll end //////////////////////////////////////////////////////////////////////// // Exit // // Print a message and exit // void Exit(int res) { printf("Exiting...
"); // CloseIfOpen();//like CloseHandle(hDevice); CloseHandle(IOWaiter); exit(res); }
//////////////////////////////////////////////////////////////////////// // Main entry point // //
int __cdecl main(int argc, char *argv[]) { int nArgIncrement = 0; int jk=0; ULONG delaynum; ULONG startaddress; ULONG endaddress; ULONG indexaddress; ULONG channelnum; ULONG rp=0; char line[256]; //=== Parameterized IOCTL Example === // int nVal; // ULONG dwVal; long i;
printf("Test application Test_pcitio starting...
");
printf("input delaynumber (hex16 : 0 to 40):
"); fgets(line, sizeof(line), stdin); sscanf (line, "%lx",&delaynum); printf("delay num is %lx
",delaynum);
printf("input startnum (hex16 : 0 to 10000):
"); fgets(line, sizeof(line), stdin); sscanf (line, "%lx",&startaddress); printf("start num is %lx
",startaddress); printf("input endnumber (hex16 : 0 to 1e400):
"); fgets(line, sizeof(line), stdin); sscanf (line, "%lx",&endaddress); printf("end num is %lx
",endaddress);
printf("input indexnumber (hex16 : 0 to ffff):
"); fgets(line, sizeof(line), stdin); sscanf (line, "%lx",&indexaddress); printf("delay num is %lx
",indexaddress);
printf("input channelnumber (hex16 : 0 to 2):
"); printf("0 : 32, 1 : 48,2 : 64 :
"); fgets(line, sizeof(line), stdin); sscanf (line, "%lx",&channelnum); printf("delay num is %lx
",channelnum); /// 加载dll库 HINSTANCE hInst; hInst=LoadLibrary("DllDevelop.dll"); DmaAdd=(TDMAHEADADD)GetProcAddress(hInst,"Test_PCITIO_IOCTL_DMAHEADADD"); DmaBytes=(TDMABYTES)GetProcAddress(hInst,"Test_PCITIO_IOCTL_DMABYTES"); startout1=(PStartout)GetProcAddress(hInst,"startout"); writesize=(PWriteSize)GetProcAddress(hInst,"writesize"); writebuf=(PWriteBuf)GetProcAddress(hInst,"writebuf"); Transdata=(PTransData)GetProcAddress(hInst,"Transdata");
//以下为模拟应用程序操作,将文件全部信息,包含头内容 //填入缓冲区
HANDLE hFile2 = NULL; if (hFile2==NULL) { hFile2 = CreateFile("TIFF26.BMP", // open MYFILE.TXT GENERIC_READ, // open for reading 0, // share for reading NULL, // no security OPEN_EXISTING, // existing file only FILE_ATTRIBUTE_NORMAL, // normal file NULL); // no attr. template }
//取得文件大小 ULONG *filelengthhigh=0; ULONG filelength=GetFileSize(hFile2,filelengthhigh); printf("file length is %ld
",filelength);
ttfbuf = (BYTE *) malloc(2048*1024); if (ttfbuf == NULL) { printf("Failed to allocate buffer for read"); Exit(1); }
ReadFile(hFile2,ttfbuf,0x3e, &rp, NULL);
// 需告诉长宽 for (i=0;i<=0x3d;i++) { printf("%x",ttfbuf); } ULONG kuany = (ttfbuf[0x19]<<24)+(ttfbuf[0x18]<<16)+(ttfbuf[0x17]<<8)+ttfbuf[0x16]; printf("
kuan is %ld
",kuany);
ULONG changx = (ttfbuf[0x15]<<24)+(ttfbuf[0x14]<<16)+(ttfbuf[0x13]<<8)+ttfbuf[0x12]; printf("chang is %ld
",changx);
writesize(changx,kuany);
ULONG dth=((filelength-0x3e)/(256*1024)); ULONG dtp=(filelength-0x3e)-(dth*256*1024); long fg=0; for (long dr=0;dr<dth;dr++) { ReadFile(hFile2,ttfbuf,256*1024, &rp, NULL); for (fg=(dr*1024*256);fg<((dr+1)*1024*256);fg++) { writebuf(fg,ttfbuf[fg-(dr*1024*256)]); } } for (i=0;i<dtp;i++) { ReadFile(hFile2,ttfbuf,1, &rp, NULL); writebuf((fg+i),ttfbuf[0]); }
//执行数据转换和发送到PCI卡函数 Transdata(channelnum); //转换完成后开始输出 startout1(delaynum,startaddress,endaddress,indexaddress,channelnum); // startout(delaynum,startaddress,endaddress,indexaddress,channelnum); CloseHandle(hFile2); return 0; }
希望北京有谁熟悉,因为可以方便调试验证,时间很紧。希望周六就能搞定(做过的人大约1小时搞定)我的电话:010-80752670
|
|