[DllImport("kernel32", SetLastError=true)] static extern unsafe IntPtr CreateFile( string FileName, // file name uint DesiredAccess, // access mode uint ShareMode, // share mode uint SecurityAttributes, // Security Attributes uint CreationDisposition, // how to create uint FlagsAndAttributes, // file attributes int hTemplateFile // handle to template file );
[DllImport("kernel32", SetLastError=true)] static extern unsafe bool ReadFile( IntPtr hFile, // handle to file void* pBuffer, // data buffer int NumberOfBytesToRead, // number of bytes to read int* pNumberOfBytesRead, // number of bytes read int Overlapped // overlapped buffer );
public bool Open(string FileName) { // open the existing file for reading handle = CreateFile( FileName, GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0);
if (handle != IntPtr.Zero) return true; else return false; }
public unsafe int Read(byte[] buffer, int index, int count) { int n = 0; fixed (byte* p = buffer) { if (!ReadFile(handle, p + index, count, &n, 0)) return 0; } return n; }
public bool Close() { // close file handle return CloseHandle(handle); } }
// compile with: /unsafe
// arguments: readfile.cs
// Use the program to read and display a text file.
using System;
using System.Runtime.InteropServices;
using System.Text;
class FileReader
{
const uint GENERIC_READ = 0x80000000;
const uint OPEN_EXISTING = 3;
IntPtr handle;
[DllImport("kernel32", SetLastError=true)]
static extern unsafe IntPtr CreateFile(
string FileName, // file name
uint DesiredAccess, // access mode
uint ShareMode, // share mode
uint SecurityAttributes, // Security Attributes
uint CreationDisposition, // how to create
uint FlagsAndAttributes, // file attributes
int hTemplateFile // handle to template file
);
[DllImport("kernel32", SetLastError=true)]
static extern unsafe bool ReadFile(
IntPtr hFile, // handle to file
void* pBuffer, // data buffer
int NumberOfBytesToRead, // number of bytes to read
int* pNumberOfBytesRead, // number of bytes read
int Overlapped // overlapped buffer
);
[DllImport("kernel32", SetLastError=true)]
static extern unsafe bool CloseHandle(
IntPtr hObject // handle to object
);
public bool Open(string FileName)
{
// open the existing file for reading
handle = CreateFile(
FileName,
GENERIC_READ,
0,
0,
OPEN_EXISTING,
0,
0);
if (handle != IntPtr.Zero)
return true;
else
return false;
}
public unsafe int Read(byte[] buffer, int index, int count)
{
int n = 0;
fixed (byte* p = buffer)
{
if (!ReadFile(handle, p + index, count, &n, 0))
return 0;
}
return n;
}
public bool Close()
{
// close file handle
return CloseHandle(handle);
}
}
unsigned char * TripleDESKey, //输入
int SourDataLen, //输入
unsigned char *SourData, //输入
unsigned char *DestData); //需要的数据 uint TripleDES(char DESType,string TripleDESKey,int SourDataLen,string SourData,StringBuilder DestData);
interop里面,输出的字符串就使StringBuilder.其他的可使MarshalAs属性来指定
有没有例子?我看过有的贴子说了“输出的字符串就使StringBuilder.其他的可使MarshalAs属性”这种方法,但也惜字如金,看了半天也没明白。先谢了。
StringBuilder来代替char*,例如:
Dllimport……//这里我略去,你自己写吧
static extern unsigned pascal TripleDES(
char DESType, //输入
StringBuilder TripleDESKey, //输入
int SourDataLen, //输入
StringBuilder SourData, //输入
StringBuilder DestData); //需要的数据
调用的时候,可以用StringBuilder来声明变量,例如:
StringBuilder strMyTemp = new StringBuilder(100);//100是你的长度