W
Wrecked
Below is the unmanaged code written with evc++
-------------------------------------------------------------------------------------------------------------------------------
#include <windows.h>
#include <windowsx.h>
#include <aygshell.h>
#include <msgqueue.h>
#include <pnp.h>
#include <diskio.h>
#include <Pkfuncs.h>
#include <sdcardddk.h>
extern "C" _declspec(dllexport) void WINAPI OnRawRead(DWORD
memory_location,unsigned char* lpOutBuf)
{
// TODO: Add your control notification handler code here
HANDLE hDevice
=CreateFile(L"DSK1:",GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
SG_REQ lpInBuf;
DWORD dwDummy = 3;
//unsigned char *lpOutBuf;
lpOutBuf = (unsigned char *)malloc(512);
memset(lpOutBuf, 0x00, 512); // only to see the changes
lpInBuf.sr_start = memory_location;//0xFF;//0x0018555; //
physical sector to read
lpInBuf.sr_num_sec = 1; // read 1 sector
lpInBuf.sr_num_sg = 1;
lpInBuf.sr_status = 0; //ERROR_SUCCESS;
lpInBuf.sr_callback =NULL;// callbackDiskRead;
lpInBuf.sr_sglist[0].sb_buf = ((LPBYTE)
MapPtrToProcess(lpOutBuf,GetCurrentProcess()));
lpInBuf.sr_sglist[0].sb_len = 512 * lpInBuf.sr_num_sec;
BOOL bRet=DeviceIoControl(hDevice, // Handle to the device
IOCTL_DISK_READ, // IOCTL for the operation
&lpInBuf, // LP to a buffer (input data)
sizeof(lpInBuf), // Size in Bytes of input data
buffer
lpOutBuf, // LP to a buffer for output data
sizeof(lpOutBuf), // Size in Bytes of output buffer
&dwDummy, // LP to variable (size of data in out
buffer)
NULL);
CloseHandle(hDevice);
}
extern "C" _declspec(dllexport) void WINAPI OnRawWrite(DWORD
memory_location,unsigned char* pBuffer)
{
HANDLE hDevice1
=CreateFile(L"DSK1:",GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
SG_REQ lpInBuf1;
DWORD dwDummy1 = 3;
char *lpOutBuf1;
lpOutBuf1 = (char *)malloc(512);
memset(lpOutBuf1, 0x00, 512); // only to see the changes
strcpy(( char*)lpOutBuf1,( char *)pBuffer);
lpInBuf1.sr_start = memory_location; //0x0018555; // physical
sector
lpInBuf1.sr_num_sec = 1; // number of sectors sector
lpInBuf1.sr_num_sg = 1;
lpInBuf1.sr_status = 0; //ERROR_SUCCESS;
lpInBuf1.sr_callback =NULL;// callbackDiskRead;
lpInBuf1.sr_sglist[0].sb_buf = ((LPBYTE)
MapPtrToProcess(lpOutBuf1,GetCurrentProcess()));
lpInBuf1.sr_sglist[0].sb_len = 512 * lpInBuf1.sr_num_sec;
BOOL bRet=DeviceIoControl(hDevice1, // Handle to the device
IOCTL_DISK_WRITE, // IOCTL for the operation
&lpInBuf1, // LP to a buffer (input data)
sizeof(lpInBuf1), // Size in Bytes of input data
buffer
lpOutBuf1, // LP to a buffer for output data
sizeof(lpOutBuf1), // Size in Bytes of output buffer
&dwDummy1, // LP to variable (size of data in out
buffer)
NULL);
CloseHandle(hDevice1);
}
-------------------------------------------------------------------------------------------------------------------------------
The functions do some basic rawread and rawwrite on an SD card.
Now how can i use this code in the c# (managed code). Specifically i
need to know how to marshal "char *".
I did try out with the following code (the dll created by evc++ was
SD_card_dll.dll. But it doesnt seam to work.
[DllImport("SD_card_dll.dll")]
public static extern void OnRawRead(int memory_location,ref
IntPtr lpOutBuf);
[DllImport("SD_card_dll.dll")]
public static extern void OnRawWrite(int memory_location,ref
IntPtr lpBuffer);
}
Thanks and Regards,
Rithesh
Student
IIIT, Bangalore
-------------------------------------------------------------------------------------------------------------------------------
#include <windows.h>
#include <windowsx.h>
#include <aygshell.h>
#include <msgqueue.h>
#include <pnp.h>
#include <diskio.h>
#include <Pkfuncs.h>
#include <sdcardddk.h>
extern "C" _declspec(dllexport) void WINAPI OnRawRead(DWORD
memory_location,unsigned char* lpOutBuf)
{
// TODO: Add your control notification handler code here
HANDLE hDevice
=CreateFile(L"DSK1:",GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
SG_REQ lpInBuf;
DWORD dwDummy = 3;
//unsigned char *lpOutBuf;
lpOutBuf = (unsigned char *)malloc(512);
memset(lpOutBuf, 0x00, 512); // only to see the changes
lpInBuf.sr_start = memory_location;//0xFF;//0x0018555; //
physical sector to read
lpInBuf.sr_num_sec = 1; // read 1 sector
lpInBuf.sr_num_sg = 1;
lpInBuf.sr_status = 0; //ERROR_SUCCESS;
lpInBuf.sr_callback =NULL;// callbackDiskRead;
lpInBuf.sr_sglist[0].sb_buf = ((LPBYTE)
MapPtrToProcess(lpOutBuf,GetCurrentProcess()));
lpInBuf.sr_sglist[0].sb_len = 512 * lpInBuf.sr_num_sec;
BOOL bRet=DeviceIoControl(hDevice, // Handle to the device
IOCTL_DISK_READ, // IOCTL for the operation
&lpInBuf, // LP to a buffer (input data)
sizeof(lpInBuf), // Size in Bytes of input data
buffer
lpOutBuf, // LP to a buffer for output data
sizeof(lpOutBuf), // Size in Bytes of output buffer
&dwDummy, // LP to variable (size of data in out
buffer)
NULL);
CloseHandle(hDevice);
}
extern "C" _declspec(dllexport) void WINAPI OnRawWrite(DWORD
memory_location,unsigned char* pBuffer)
{
HANDLE hDevice1
=CreateFile(L"DSK1:",GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
SG_REQ lpInBuf1;
DWORD dwDummy1 = 3;
char *lpOutBuf1;
lpOutBuf1 = (char *)malloc(512);
memset(lpOutBuf1, 0x00, 512); // only to see the changes
strcpy(( char*)lpOutBuf1,( char *)pBuffer);
lpInBuf1.sr_start = memory_location; //0x0018555; // physical
sector
lpInBuf1.sr_num_sec = 1; // number of sectors sector
lpInBuf1.sr_num_sg = 1;
lpInBuf1.sr_status = 0; //ERROR_SUCCESS;
lpInBuf1.sr_callback =NULL;// callbackDiskRead;
lpInBuf1.sr_sglist[0].sb_buf = ((LPBYTE)
MapPtrToProcess(lpOutBuf1,GetCurrentProcess()));
lpInBuf1.sr_sglist[0].sb_len = 512 * lpInBuf1.sr_num_sec;
BOOL bRet=DeviceIoControl(hDevice1, // Handle to the device
IOCTL_DISK_WRITE, // IOCTL for the operation
&lpInBuf1, // LP to a buffer (input data)
sizeof(lpInBuf1), // Size in Bytes of input data
buffer
lpOutBuf1, // LP to a buffer for output data
sizeof(lpOutBuf1), // Size in Bytes of output buffer
&dwDummy1, // LP to variable (size of data in out
buffer)
NULL);
CloseHandle(hDevice1);
}
-------------------------------------------------------------------------------------------------------------------------------
The functions do some basic rawread and rawwrite on an SD card.
Now how can i use this code in the c# (managed code). Specifically i
need to know how to marshal "char *".
I did try out with the following code (the dll created by evc++ was
SD_card_dll.dll. But it doesnt seam to work.
[DllImport("SD_card_dll.dll")]
public static extern void OnRawRead(int memory_location,ref
IntPtr lpOutBuf);
[DllImport("SD_card_dll.dll")]
public static extern void OnRawWrite(int memory_location,ref
IntPtr lpBuffer);
}
Thanks and Regards,
Rithesh
Student
IIIT, Bangalore