W
Wrecked
Hello,
I was trying to use the CeCreateFile function of rapi.dll in my c#
code.. All i am trying to do in the program bellow is to create a
file..
but when i try to run the program i get a MissingMethodException -
"Can't find PInvoke DLL 'rapi.dll'."
Is there something wrong with code -- and do we need to place rapi.dll
at some specific location(currently i have placed in the same folder
where i am deploying my project).
I am not very familiar with P/Invoke.. you help will be greatly
appreciated.
Thanks
Rithesh
static void Main()
{
MessageBox.Show("Before Invoking");
short FILE_ATTRIBUTE_NORMAL = 0x80;
short FILE_ATTRIBUTE_DIRECTORY = 0x10;
short FILE_ATTRIBUTE_TEMPORARY = 0x100;
uint GENERIC_READ = 0x80000000;
uint GENERIC_WRITE = 0x40000000;
short CREATE_NEW = 1;
short CREATE_ALWAYS = 2;
short OPEN_EXISTING = 3;
int handle = CeCreateFile("xxxx", GENERIC_WRITE, 0, 0,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
MessageBox.Show("Card handle is - " + handle);
// End of card related code //
}
[StructLayout(LayoutKind.Sequential)]
public struct OSVERSIONINFO
{
public int dwOSVersionInfoSize;
public int dwMajorVersion;
public int dwMinorVersion;
public int dwBuildNumber;
public int dwPlatformId;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string szCSDVersion;
}
[StructLayout(LayoutKind.Sequential)]
public struct RAPIINIT
{
int cbSize;
int heRapiInit;
int hrRapiInit;
}
[StructLayout(LayoutKind.Sequential)]
public struct SECURITY_ATTRIBUTES
{
public int nLength;
public int lpSecurityDescriptor;
public int bInheritHandle;
}
[DllImport("rapi.dll", CharSet = CharSet.Unicode)]
public static extern int CeCloseHandle(int hObject);
[DllImport("rapi.dll", CharSet = CharSet.Unicode)]
public static extern int CeCreateFile(string lpFileName, uint
dwDesiredAccess, int dwShareMode, int lpSecurityAttributes, int
dwCreationDisposition, int dwFlagsAndAttributes, int
hTemplateFile);
[DllImport("rapi.dll", CharSet = CharSet.Unicode)]
public static extern int
CeRapiInitEx([MarshalAs(UnmanagedType.Struct)] ref RAPIINIT pRapiInit);
[DllImport("rapi.dll", CharSet = CharSet.Unicode)]
public static extern int CeRapiInit();
[DllImport("rapi.dll", CharSet = CharSet.Unicode)]
public static extern int CeRapiUninit();
[DllImport("rapi.dll", CharSet = CharSet.Unicode)]
public static extern int CeReadFile(int hFile, StringBuilder
lpBuffer, int nNumberOfbytesToRead, out int lpNumberOfbytesRead, int
lpOverlapped);
[DllImport("rapi.dll", CharSet = CharSet.Unicode)]
public static extern int CeWriteFile(int hFile, StringBuilder
lpBuffer, int nNumberOfbytesToWrite, out int lpNumberOfbytesWritten,
int lpOverlapped);
[DllImport("rapi.dll", CharSet = CharSet.Unicode)]
public static extern int GetLastError();
[DllImport("rapi.dll", CharSet = CharSet.Unicode)]
public static extern int CeSetEndOfFile(int hFile);
[DllImport("rapi.dll", CharSet = CharSet.Unicode)]
public static extern int CeGetFileSize(int hFile, int
lpFileSizeHigh);
[DllImport("rapi.dll", CharSet = CharSet.Unicode)]
public static extern int CeGetLastError();
}
I was trying to use the CeCreateFile function of rapi.dll in my c#
code.. All i am trying to do in the program bellow is to create a
file..
but when i try to run the program i get a MissingMethodException -
"Can't find PInvoke DLL 'rapi.dll'."
Is there something wrong with code -- and do we need to place rapi.dll
at some specific location(currently i have placed in the same folder
where i am deploying my project).
I am not very familiar with P/Invoke.. you help will be greatly
appreciated.
Thanks
Rithesh
static void Main()
{
MessageBox.Show("Before Invoking");
short FILE_ATTRIBUTE_NORMAL = 0x80;
short FILE_ATTRIBUTE_DIRECTORY = 0x10;
short FILE_ATTRIBUTE_TEMPORARY = 0x100;
uint GENERIC_READ = 0x80000000;
uint GENERIC_WRITE = 0x40000000;
short CREATE_NEW = 1;
short CREATE_ALWAYS = 2;
short OPEN_EXISTING = 3;
int handle = CeCreateFile("xxxx", GENERIC_WRITE, 0, 0,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
MessageBox.Show("Card handle is - " + handle);
// End of card related code //
}
[StructLayout(LayoutKind.Sequential)]
public struct OSVERSIONINFO
{
public int dwOSVersionInfoSize;
public int dwMajorVersion;
public int dwMinorVersion;
public int dwBuildNumber;
public int dwPlatformId;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string szCSDVersion;
}
[StructLayout(LayoutKind.Sequential)]
public struct RAPIINIT
{
int cbSize;
int heRapiInit;
int hrRapiInit;
}
[StructLayout(LayoutKind.Sequential)]
public struct SECURITY_ATTRIBUTES
{
public int nLength;
public int lpSecurityDescriptor;
public int bInheritHandle;
}
[DllImport("rapi.dll", CharSet = CharSet.Unicode)]
public static extern int CeCloseHandle(int hObject);
[DllImport("rapi.dll", CharSet = CharSet.Unicode)]
public static extern int CeCreateFile(string lpFileName, uint
dwDesiredAccess, int dwShareMode, int lpSecurityAttributes, int
dwCreationDisposition, int dwFlagsAndAttributes, int
hTemplateFile);
[DllImport("rapi.dll", CharSet = CharSet.Unicode)]
public static extern int
CeRapiInitEx([MarshalAs(UnmanagedType.Struct)] ref RAPIINIT pRapiInit);
[DllImport("rapi.dll", CharSet = CharSet.Unicode)]
public static extern int CeRapiInit();
[DllImport("rapi.dll", CharSet = CharSet.Unicode)]
public static extern int CeRapiUninit();
[DllImport("rapi.dll", CharSet = CharSet.Unicode)]
public static extern int CeReadFile(int hFile, StringBuilder
lpBuffer, int nNumberOfbytesToRead, out int lpNumberOfbytesRead, int
lpOverlapped);
[DllImport("rapi.dll", CharSet = CharSet.Unicode)]
public static extern int CeWriteFile(int hFile, StringBuilder
lpBuffer, int nNumberOfbytesToWrite, out int lpNumberOfbytesWritten,
int lpOverlapped);
[DllImport("rapi.dll", CharSet = CharSet.Unicode)]
public static extern int GetLastError();
[DllImport("rapi.dll", CharSet = CharSet.Unicode)]
public static extern int CeSetEndOfFile(int hFile);
[DllImport("rapi.dll", CharSet = CharSet.Unicode)]
public static extern int CeGetFileSize(int hFile, int
lpFileSizeHigh);
[DllImport("rapi.dll", CharSet = CharSet.Unicode)]
public static extern int CeGetLastError();
}