Help for Marshalling

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
 
G

Guest

Well a char * would likely be a byte array, though your code is not
conducive to that. You're using a strcpy on it, without knowing a length
which is generally very unsafe (this is how many security holes in the
desktop OS are created and exploited).

You code appears to assume ASCII string input. Also not the best design for
CE - especially when you need to marshal from managed code where strings are
always Unicode.

To keep with your definitions, change your P/Invoke to a byte[], then take
your string data, pass it through Encoding.ASCII.GetBytes and pass that
through.


--
Chris Tacke
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--



Wrecked said:
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
 
W

Wrecked

Hello Chris,
From one of the previous discussions i understand that i could as well
use OpenNetCF to use createfile and deviceIocontrol in the
StreamInterfaceDriver. Kindly if u could throw some light on how this
could be done, it would be great. I couldnt find much documentation for
this.
All i could know about this class was one could use this abstract base
class to create wrappers around Stream Interface Drivers that are not
supported by the CF. I am not able to figure out how i could pass
SG_REQ structure.

It would help me a lot to use the functions OpenNetCF since then i
wouldnt have to worry about deploying my project on phones with
different architectures. I am not an expert on .net CF, a skeleton flow
of how i could write this would be of great help.

Thanks and Regards,
Rithesh
Student,
IIIT, Bangalore

Well a char * would likely be a byte array, though your code is not
conducive to that. You're using a strcpy on it, without knowing a length
which is generally very unsafe (this is how many security holes in the
desktop OS are created and exploited).

You code appears to assume ASCII string input. Also not the best design for
CE - especially when you need to marshal from managed code where strings are
always Unicode.

To keep with your definitions, change your P/Invoke to a byte[], then take
your string data, pass it through Encoding.ASCII.GetBytes and pass that
through.


--
Chris Tacke
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--



Wrecked said:
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
 
G

Guest

You'd simply derive from StreamInterfaceDriver and set the prefix to DSK1:
and that gives you the foundation. Create a function for reading and have
it call its internal Read, and the same for Write.

For reading or writing any specific structure, you need to define the
sructure (or class) and then pass it, following hte rules of CF marshaling
appropriate for your version.


--
Chris Tacke
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--


Wrecked said:
Hello Chris,
From one of the previous discussions i understand that i could as well
use OpenNetCF to use createfile and deviceIocontrol in the
StreamInterfaceDriver. Kindly if u could throw some light on how this
could be done, it would be great. I couldnt find much documentation for
this.
All i could know about this class was one could use this abstract base
class to create wrappers around Stream Interface Drivers that are not
supported by the CF. I am not able to figure out how i could pass
SG_REQ structure.

It would help me a lot to use the functions OpenNetCF since then i
wouldnt have to worry about deploying my project on phones with
different architectures. I am not an expert on .net CF, a skeleton flow
of how i could write this would be of great help.

Thanks and Regards,
Rithesh
Student,
IIIT, Bangalore

Well a char * would likely be a byte array, though your code is not
conducive to that. You're using a strcpy on it, without knowing a length
which is generally very unsafe (this is how many security holes in the
desktop OS are created and exploited).

You code appears to assume ASCII string input. Also not the best design
for
CE - especially when you need to marshal from managed code where strings
are
always Unicode.

To keep with your definitions, change your P/Invoke to a byte[], then
take
your string data, pass it through Encoding.ASCII.GetBytes and pass that
through.


--
Chris Tacke
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--



Wrecked said:
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
 
W

Wrecked

Hello Chris,
Thanks again for your help. I am still trying to hook up with C#
and excuse me if u find my questions too simple. I couldnt exactly
follow - "set the prefix to DSK1:", kindly could u be a bit more
specific as how i could do that.

Staying with c#, to access specific locations on DSK1 to read/write(i.e
after creating an handle to it - which i am yet to figure out how),
could i use PhysicalAddressPointer(int,int) and then use
ReadByte/WriteByte in PhysicalAddressPointer class. Is this also an
alternative to DeviceIoControl and Createfile functions.

Thanks and Regards,
Rithesh
Student,
IIIT, Bangalore


You'd simply derive from StreamInterfaceDriver and set the prefix to DSK1:
and that gives you the foundation. Create a function for reading and have
it call its internal Read, and the same for Write.

For reading or writing any specific structure, you need to define the
sructure (or class) and then pass it, following hte rules of CF marshaling
appropriate for your version.


--
Chris Tacke
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--


Wrecked said:
Hello Chris,
From one of the previous discussions i understand that i could as well
use OpenNetCF to use createfile and deviceIocontrol in the
StreamInterfaceDriver. Kindly if u could throw some light on how this
could be done, it would be great. I couldnt find much documentation for
this.
All i could know about this class was one could use this abstract base
class to create wrappers around Stream Interface Drivers that are not
supported by the CF. I am not able to figure out how i could pass
SG_REQ structure.

It would help me a lot to use the functions OpenNetCF since then i
wouldnt have to worry about deploying my project on phones with
different architectures. I am not an expert on .net CF, a skeleton flow
of how i could write this would be of great help.

Thanks and Regards,
Rithesh
Student,
IIIT, Bangalore

Well a char * would likely be a byte array, though your code is not
conducive to that. You're using a strcpy on it, without knowing a length
which is generally very unsafe (this is how many security holes in the
desktop OS are created and exploited).

You code appears to assume ASCII string input. Also not the best design
for
CE - especially when you need to marshal from managed code where strings
are
always Unicode.

To keep with your definitions, change your P/Invoke to a byte[], then
take
your string data, pass it through Encoding.ASCII.GetBytes and pass that
through.


--
Chris Tacke
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--



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
 
G

Guest

If you look at the StreamInterfaceDriver class, it's ctor takes a device
prefix, which is what it passes to CreateFile.

I'd discourage using the PhysicalAddressPointer for a few reasons:
1. it circumvents any existing driver, so you could get conflicts
2. it's not supported in CE 6.0
3. disks don't really work that way


--
Chris Tacke
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--



Wrecked said:
Hello Chris,
Thanks again for your help. I am still trying to hook up with C#
and excuse me if u find my questions too simple. I couldnt exactly
follow - "set the prefix to DSK1:", kindly could u be a bit more
specific as how i could do that.

Staying with c#, to access specific locations on DSK1 to read/write(i.e
after creating an handle to it - which i am yet to figure out how),
could i use PhysicalAddressPointer(int,int) and then use
ReadByte/WriteByte in PhysicalAddressPointer class. Is this also an
alternative to DeviceIoControl and Createfile functions.

Thanks and Regards,
Rithesh
Student,
IIIT, Bangalore


You'd simply derive from StreamInterfaceDriver and set the prefix to
DSK1:
and that gives you the foundation. Create a function for reading and
have
it call its internal Read, and the same for Write.

For reading or writing any specific structure, you need to define the
sructure (or class) and then pass it, following hte rules of CF
marshaling
appropriate for your version.


--
Chris Tacke
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--


Wrecked said:
Hello Chris,

From one of the previous discussions i understand that i could as well
use OpenNetCF to use createfile and deviceIocontrol in the
StreamInterfaceDriver. Kindly if u could throw some light on how this
could be done, it would be great. I couldnt find much documentation for
this.
All i could know about this class was one could use this abstract base
class to create wrappers around Stream Interface Drivers that are not
supported by the CF. I am not able to figure out how i could pass
SG_REQ structure.

It would help me a lot to use the functions OpenNetCF since then i
wouldnt have to worry about deploying my project on phones with
different architectures. I am not an expert on .net CF, a skeleton flow
of how i could write this would be of great help.

Thanks and Regards,
Rithesh
Student,
IIIT, Bangalore

<ctacke/> wrote:
Well a char * would likely be a byte array, though your code is not
conducive to that. You're using a strcpy on it, without knowing a
length
which is generally very unsafe (this is how many security holes in the
desktop OS are created and exploited).

You code appears to assume ASCII string input. Also not the best
design
for
CE - especially when you need to marshal from managed code where
strings
are
always Unicode.

To keep with your definitions, change your P/Invoke to a byte[], then
take
your string data, pass it through Encoding.ASCII.GetBytes and pass
that
through.


--
Chris Tacke
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--



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
 
W

Wrecked

Chris,
After i pass the DSK1 to the ctor, let me know if this approach is
valid. To write/read to a specific location-
I open() it, then seek() to the required location and then use
read()/write() and then close() it.

Thanks and Regards
Rithesh
Student,
IIIT, Bangalore



If you look at the StreamInterfaceDriver class, it's ctor takes a device
prefix, which is what it passes to CreateFile.

I'd discourage using the PhysicalAddressPointer for a few reasons:
1. it circumvents any existing driver, so you could get conflicts
2. it's not supported in CE 6.0
3. disks don't really work that way


--
Chris Tacke
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--



Wrecked said:
Hello Chris,
Thanks again for your help. I am still trying to hook up with C#
and excuse me if u find my questions too simple. I couldnt exactly
follow - "set the prefix to DSK1:", kindly could u be a bit more
specific as how i could do that.

Staying with c#, to access specific locations on DSK1 to read/write(i.e
after creating an handle to it - which i am yet to figure out how),
could i use PhysicalAddressPointer(int,int) and then use
ReadByte/WriteByte in PhysicalAddressPointer class. Is this also an
alternative to DeviceIoControl and Createfile functions.

Thanks and Regards,
Rithesh
Student,
IIIT, Bangalore


You'd simply derive from StreamInterfaceDriver and set the prefix to
DSK1:
and that gives you the foundation. Create a function for reading and
have
it call its internal Read, and the same for Write.

For reading or writing any specific structure, you need to define the
sructure (or class) and then pass it, following hte rules of CF
marshaling
appropriate for your version.


--
Chris Tacke
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--


Hello Chris,

From one of the previous discussions i understand that i could as well
use OpenNetCF to use createfile and deviceIocontrol in the
StreamInterfaceDriver. Kindly if u could throw some light on how this
could be done, it would be great. I couldnt find much documentation for
this.
All i could know about this class was one could use this abstract base
class to create wrappers around Stream Interface Drivers that are not
supported by the CF. I am not able to figure out how i could pass
SG_REQ structure.

It would help me a lot to use the functions OpenNetCF since then i
wouldnt have to worry about deploying my project on phones with
different architectures. I am not an expert on .net CF, a skeleton flow
of how i could write this would be of great help.

Thanks and Regards,
Rithesh
Student,
IIIT, Bangalore

<ctacke/> wrote:
Well a char * would likely be a byte array, though your code is not
conducive to that. You're using a strcpy on it, without knowing a
length
which is generally very unsafe (this is how many security holes in the
desktop OS are created and exploited).

You code appears to assume ASCII string input. Also not the best
design
for
CE - especially when you need to marshal from managed code where
strings
are
always Unicode.

To keep with your definitions, change your P/Invoke to a byte[], then
take
your string data, pass it through Encoding.ASCII.GetBytes and pass
that
through.


--
Chris Tacke
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--



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
 
G

Guest

I've not used the DSK driver to do this, so I'm not sure if that is how it
work. Intuitively that sounds right, but to know for sure I'd go look in PB
for examples that do use the driver and see what's going on there, or just
try that with known data to see if it works.

This is especially true for Flahs, as it has to be put into write mode
before a write occurs. Maybe the driver does that - maybe not. And if it's
a TFAT system I'd be interested to see how that works. There are a whole
lot of unknowns here without doing research, and all in all I can't see what
good this is for anyway. For disk access there is a file system, and that's
what you should be using. Doing direct reads and writes sounds like a
recipe for a corrupt file system to me.

--
Chris Tacke
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--


Wrecked said:
Chris,
After i pass the DSK1 to the ctor, let me know if this approach is
valid. To write/read to a specific location-
I open() it, then seek() to the required location and then use
read()/write() and then close() it.

Thanks and Regards
Rithesh
Student,
IIIT, Bangalore



If you look at the StreamInterfaceDriver class, it's ctor takes a device
prefix, which is what it passes to CreateFile.

I'd discourage using the PhysicalAddressPointer for a few reasons:
1. it circumvents any existing driver, so you could get conflicts
2. it's not supported in CE 6.0
3. disks don't really work that way


--
Chris Tacke
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--



Wrecked said:
Hello Chris,
Thanks again for your help. I am still trying to hook up with C#
and excuse me if u find my questions too simple. I couldnt exactly
follow - "set the prefix to DSK1:", kindly could u be a bit more
specific as how i could do that.

Staying with c#, to access specific locations on DSK1 to read/write(i.e
after creating an handle to it - which i am yet to figure out how),
could i use PhysicalAddressPointer(int,int) and then use
ReadByte/WriteByte in PhysicalAddressPointer class. Is this also an
alternative to DeviceIoControl and Createfile functions.

Thanks and Regards,
Rithesh
Student,
IIIT, Bangalore


<ctacke/> wrote:
You'd simply derive from StreamInterfaceDriver and set the prefix to
DSK1:
and that gives you the foundation. Create a function for reading and
have
it call its internal Read, and the same for Write.

For reading or writing any specific structure, you need to define the
sructure (or class) and then pass it, following hte rules of CF
marshaling
appropriate for your version.


--
Chris Tacke
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--


Hello Chris,

From one of the previous discussions i understand that i could as
well
use OpenNetCF to use createfile and deviceIocontrol in the
StreamInterfaceDriver. Kindly if u could throw some light on how
this
could be done, it would be great. I couldnt find much documentation
for
this.
All i could know about this class was one could use this abstract
base
class to create wrappers around Stream Interface Drivers that are
not
supported by the CF. I am not able to figure out how i could pass
SG_REQ structure.

It would help me a lot to use the functions OpenNetCF since then i
wouldnt have to worry about deploying my project on phones with
different architectures. I am not an expert on .net CF, a skeleton
flow
of how i could write this would be of great help.

Thanks and Regards,
Rithesh
Student,
IIIT, Bangalore

<ctacke/> wrote:
Well a char * would likely be a byte array, though your code is not
conducive to that. You're using a strcpy on it, without knowing a
length
which is generally very unsafe (this is how many security holes in
the
desktop OS are created and exploited).

You code appears to assume ASCII string input. Also not the best
design
for
CE - especially when you need to marshal from managed code where
strings
are
always Unicode.

To keep with your definitions, change your P/Invoke to a byte[],
then
take
your string data, pass it through Encoding.ASCII.GetBytes and pass
that
through.


--
Chris Tacke
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--



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
 
W

Wrecked

Chris,
Thanks a lot for you help. I will keep you posted if this thing
actually works.. else may be i will have to use the DLL approach.

Thanks and regards,
Rithesh
Student,
IIIT, Bangalore

I've not used the DSK driver to do this, so I'm not sure if that is how it
work. Intuitively that sounds right, but to know for sure I'd go look in PB
for examples that do use the driver and see what's going on there, or just
try that with known data to see if it works.

This is especially true for Flahs, as it has to be put into write mode
before a write occurs. Maybe the driver does that - maybe not. And if it's
a TFAT system I'd be interested to see how that works. There are a whole
lot of unknowns here without doing research, and all in all I can't see what
good this is for anyway. For disk access there is a file system, and that's
what you should be using. Doing direct reads and writes sounds like a
recipe for a corrupt file system to me.

--
Chris Tacke
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--


Wrecked said:
Chris,
After i pass the DSK1 to the ctor, let me know if this approach is
valid. To write/read to a specific location-
I open() it, then seek() to the required location and then use
read()/write() and then close() it.

Thanks and Regards
Rithesh
Student,
IIIT, Bangalore



If you look at the StreamInterfaceDriver class, it's ctor takes a device
prefix, which is what it passes to CreateFile.

I'd discourage using the PhysicalAddressPointer for a few reasons:
1. it circumvents any existing driver, so you could get conflicts
2. it's not supported in CE 6.0
3. disks don't really work that way


--
Chris Tacke
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--



Hello Chris,
Thanks again for your help. I am still trying to hook up with C#
and excuse me if u find my questions too simple. I couldnt exactly
follow - "set the prefix to DSK1:", kindly could u be a bit more
specific as how i could do that.

Staying with c#, to access specific locations on DSK1 to read/write(i.e
after creating an handle to it - which i am yet to figure out how),
could i use PhysicalAddressPointer(int,int) and then use
ReadByte/WriteByte in PhysicalAddressPointer class. Is this also an
alternative to DeviceIoControl and Createfile functions.

Thanks and Regards,
Rithesh
Student,
IIIT, Bangalore


<ctacke/> wrote:
You'd simply derive from StreamInterfaceDriver and set the prefix to
DSK1:
and that gives you the foundation. Create a function for reading and
have
it call its internal Read, and the same for Write.

For reading or writing any specific structure, you need to define the
sructure (or class) and then pass it, following hte rules of CF
marshaling
appropriate for your version.


--
Chris Tacke
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--


Hello Chris,

From one of the previous discussions i understand that i could as
well
use OpenNetCF to use createfile and deviceIocontrol in the
StreamInterfaceDriver. Kindly if u could throw some light on how
this
could be done, it would be great. I couldnt find much documentation
for
this.
All i could know about this class was one could use this abstract
base
class to create wrappers around Stream Interface Drivers that are
not
supported by the CF. I am not able to figure out how i could pass
SG_REQ structure.

It would help me a lot to use the functions OpenNetCF since then i
wouldnt have to worry about deploying my project on phones with
different architectures. I am not an expert on .net CF, a skeleton
flow
of how i could write this would be of great help.

Thanks and Regards,
Rithesh
Student,
IIIT, Bangalore

<ctacke/> wrote:
Well a char * would likely be a byte array, though your code is not
conducive to that. You're using a strcpy on it, without knowing a
length
which is generally very unsafe (this is how many security holes in
the
desktop OS are created and exploited).

You code appears to assume ASCII string input. Also not the best
design
for
CE - especially when you need to marshal from managed code where
strings
are
always Unicode.

To keep with your definitions, change your P/Invoke to a byte[],
then
take
your string data, pass it through Encoding.ASCII.GetBytes and pass
that
through.


--
Chris Tacke
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--



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
 
W

Wrecked

Chris,
Thanks a lot for your help. I will keep you posted if this thing
actually works.. else may be i will have to use the DLL approach.

Thanks and regards,
Rithesh
Student,
IIIT, Bangalore

I've not used the DSK driver to do this, so I'm not sure if that is how it
work. Intuitively that sounds right, but to know for sure I'd go look in PB
for examples that do use the driver and see what's going on there, or just
try that with known data to see if it works.

This is especially true for Flahs, as it has to be put into write mode
before a write occurs. Maybe the driver does that - maybe not. And if it's
a TFAT system I'd be interested to see how that works. There are a whole
lot of unknowns here without doing research, and all in all I can't see what
good this is for anyway. For disk access there is a file system, and that's
what you should be using. Doing direct reads and writes sounds like a
recipe for a corrupt file system to me.

--
Chris Tacke
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--


Wrecked said:
Chris,
After i pass the DSK1 to the ctor, let me know if this approach is
valid. To write/read to a specific location-
I open() it, then seek() to the required location and then use
read()/write() and then close() it.

Thanks and Regards
Rithesh
Student,
IIIT, Bangalore



If you look at the StreamInterfaceDriver class, it's ctor takes a device
prefix, which is what it passes to CreateFile.

I'd discourage using the PhysicalAddressPointer for a few reasons:
1. it circumvents any existing driver, so you could get conflicts
2. it's not supported in CE 6.0
3. disks don't really work that way


--
Chris Tacke
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--



Hello Chris,
Thanks again for your help. I am still trying to hook up with C#
and excuse me if u find my questions too simple. I couldnt exactly
follow - "set the prefix to DSK1:", kindly could u be a bit more
specific as how i could do that.

Staying with c#, to access specific locations on DSK1 to read/write(i.e
after creating an handle to it - which i am yet to figure out how),
could i use PhysicalAddressPointer(int,int) and then use
ReadByte/WriteByte in PhysicalAddressPointer class. Is this also an
alternative to DeviceIoControl and Createfile functions.

Thanks and Regards,
Rithesh
Student,
IIIT, Bangalore


<ctacke/> wrote:
You'd simply derive from StreamInterfaceDriver and set the prefix to
DSK1:
and that gives you the foundation. Create a function for reading and
have
it call its internal Read, and the same for Write.

For reading or writing any specific structure, you need to define the
sructure (or class) and then pass it, following hte rules of CF
marshaling
appropriate for your version.


--
Chris Tacke
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--


Hello Chris,

From one of the previous discussions i understand that i could as
well
use OpenNetCF to use createfile and deviceIocontrol in the
StreamInterfaceDriver. Kindly if u could throw some light on how
this
could be done, it would be great. I couldnt find much documentation
for
this.
All i could know about this class was one could use this abstract
base
class to create wrappers around Stream Interface Drivers that are
not
supported by the CF. I am not able to figure out how i could pass
SG_REQ structure.

It would help me a lot to use the functions OpenNetCF since then i
wouldnt have to worry about deploying my project on phones with
different architectures. I am not an expert on .net CF, a skeleton
flow
of how i could write this would be of great help.

Thanks and Regards,
Rithesh
Student,
IIIT, Bangalore

<ctacke/> wrote:
Well a char * would likely be a byte array, though your code is not
conducive to that. You're using a strcpy on it, without knowing a
length
which is generally very unsafe (this is how many security holes in
the
desktop OS are created and exploited).

You code appears to assume ASCII string input. Also not the best
design
for
CE - especially when you need to marshal from managed code where
strings
are
always Unicode.

To keep with your definitions, change your P/Invoke to a byte[],
then
take
your string data, pass it through Encoding.ASCII.GetBytes and pass
that
through.


--
Chris Tacke
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--



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
 
W

Wrecked

Hello Chris,
Just out of curiosity how do u actually send back a string from
a evc++ dll to the managed code.. as an example i was trying to use the
following code which i had given above.. but i am getting not getting
any thing back

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
extern "C" _declspec(dllexport) unsigned char* WINAPI Read()
{

unsigned char *lpOutBuf;
lpOutBuf = (unsigned char *)malloc(512);
memset(lpOutBuf, 0x97, 512);
return lpOutBuf;

}

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

and on the c# side i am importing as--

string t = Read();
..
..
..
..

[DllImport("SD_card_dll.dll",EntryPoint="Read")]
public static extern string Read();


The code runs without any exceptions but it only returns a null...

Whats wrong with the code.. or is there a better way to do it??


Thanks and Regards,
Rithesh
Student,
IIIT, Bangalore



Chris,
Thanks a lot for your help. I will keep you posted if this thing
actually works.. else may be i will have to use the DLL approach.

Thanks and regards,
Rithesh
Student,
IIIT, Bangalore

I've not used the DSK driver to do this, so I'm not sure if that is howit
work. Intuitively that sounds right, but to know for sure I'd go look in PB
for examples that do use the driver and see what's going on there, or just
try that with known data to see if it works.
This is especially true for Flahs, as it has to be put into write mode
before a write occurs. Maybe the driver does that - maybe not. And ifit's
a TFAT system I'd be interested to see how that works. There are a whole
lot of unknowns here without doing research, and all in all I can't seewhat
good this is for anyway. For disk access there is a file system, and that's
what you should be using. Doing direct reads and writes sounds like a
recipe for a corrupt file system to me.
--
Chris Tacke
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--
Wrecked said:
Chris,
After i pass the DSK1 to the ctor, let me know if this approach is
valid. To write/read to a specific location-
I open() it, then seek() to the required location and then use
read()/write() and then close() it.
Thanks and Regards
Rithesh
Student,
IIIT, Bangalore
<ctacke/> wrote:
If you look at the StreamInterfaceDriver class, it's ctor takes a device
prefix, which is what it passes to CreateFile.
I'd discourage using the PhysicalAddressPointer for a few reasons:
1. it circumvents any existing driver, so you could get conflicts
2. it's not supported in CE 6.0
3. disks don't really work that way
--
Chris Tacke
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--
Hello Chris,
Thanks again for your help. I am still trying to hook up with C#
and excuse me if u find my questions too simple. I couldnt exactly
follow - "set the prefix to DSK1:", kindly could u be a bit more
specific as how i could do that.
Staying with c#, to access specific locations on DSK1 to read/write(i.e
after creating an handle to it - which i am yet to figure out how),
could i use PhysicalAddressPointer(int,int) and then use
ReadByte/WriteByte in PhysicalAddressPointer class. Is this also an
alternative to DeviceIoControl and Createfile functions.
Thanks and Regards,
Rithesh
Student,
IIIT, Bangalore
<ctacke/> wrote:
You'd simply derive from StreamInterfaceDriver and set the prefixto
DSK1:
and that gives you the foundation. Create a function for readingand
have
it call its internal Read, and the same for Write.
For reading or writing any specific structure, you need to definethe
sructure (or class) and then pass it, following hte rules of CF
marshaling
appropriate for your version.
--
Chris Tacke
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--
Hello Chris,
From one of the previous discussions i understand that i could as
well
use OpenNetCF to use createfile and deviceIocontrol in the
StreamInterfaceDriver. Kindly if u could throw some light on how
this
could be done, it would be great. I couldnt find much documentation
for
this.
All i could know about this class was one could use this abstract
base
class to create wrappers around Stream Interface Drivers that are
not
supported by the CF. I am not able to figure out how i could pass
SG_REQ structure.
It would help me a lot to use the functions OpenNetCF since then i
wouldnt have to worry about deploying my project on phones with
different architectures. I am not an expert on .net CF, a skeleton
flow
of how i could write this would be of great help.
Thanks and Regards,
Rithesh
Student,
IIIT, Bangalore
<ctacke/> wrote:
Well a char * would likely be a byte array, though your code is not
conducive to that. You're using a strcpy on it, without knowing a
length
which is generally very unsafe (this is how many security holes in
the
desktop OS are created and exploited).
You code appears to assume ASCII string input. Also not the best
design
for
CE - especially when you need to marshal from managed code where
strings
are
always Unicode.
To keep with your definitions, change your P/Invoke to a byte[],
then
take
your string data, pass it through Encoding.ASCII.GetBytes and pass
that
through.
--
Chris Tacke
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--
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 inout
buffer)
NULL);...

read more »
 
G

Guest

This occurs often enough I really need to blog it. It is rarely, if ever, a
good idea to return a string from a function, as controlling ownership of
the memory is an issue and you will often result in memory leaks or hard to
maintain code. If you need to "return" a string, pass a string pointer as a
parameter, along with another that tells how big that buffer is so the
caller maintains ownership.


--
Chris Tacke
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--


Hello Chris,
Just out of curiosity how do u actually send back a string from
a evc++ dll to the managed code.. as an example i was trying to use the
following code which i had given above.. but i am getting not getting
any thing back

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
extern "C" _declspec(dllexport) unsigned char* WINAPI Read()
{

unsigned char *lpOutBuf;
lpOutBuf = (unsigned char *)malloc(512);
memset(lpOutBuf, 0x97, 512);
return lpOutBuf;

}

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

and on the c# side i am importing as--

string t = Read();
..
..
..
..

[DllImport("SD_card_dll.dll",EntryPoint="Read")]
public static extern string Read();


The code runs without any exceptions but it only returns a null...

Whats wrong with the code.. or is there a better way to do it??


Thanks and Regards,
Rithesh
Student,
IIIT, Bangalore



Chris,
Thanks a lot for your help. I will keep you posted if this thing
actually works.. else may be i will have to use the DLL approach.

Thanks and regards,
Rithesh
Student,
IIIT, Bangalore

I've not used the DSK driver to do this, so I'm not sure if that is how
it
work. Intuitively that sounds right, but to know for sure I'd go look
in PB
for examples that do use the driver and see what's going on there, or
just
try that with known data to see if it works.
This is especially true for Flahs, as it has to be put into write mode
before a write occurs. Maybe the driver does that - maybe not. And if
it's
a TFAT system I'd be interested to see how that works. There are a
whole
lot of unknowns here without doing research, and all in all I can't see
what
good this is for anyway. For disk access there is a file system, and
that's
what you should be using. Doing direct reads and writes sounds like a
recipe for a corrupt file system to me.
--
Chris Tacke
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--
Wrecked said:
Chris,
After i pass the DSK1 to the ctor, let me know if this approach is
valid. To write/read to a specific location-
I open() it, then seek() to the required location and then use
read()/write() and then close() it.
Thanks and Regards
Rithesh
Student,
IIIT, Bangalore
<ctacke/> wrote:
If you look at the StreamInterfaceDriver class, it's ctor takes a
device
prefix, which is what it passes to CreateFile.
I'd discourage using the PhysicalAddressPointer for a few reasons:
1. it circumvents any existing driver, so you could get conflicts
2. it's not supported in CE 6.0
3. disks don't really work that way
--
Chris Tacke
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--
Hello Chris,
Thanks again for your help. I am still trying to hook up with
C#
and excuse me if u find my questions too simple. I couldnt exactly
follow - "set the prefix to DSK1:", kindly could u be a bit more
specific as how i could do that.
Staying with c#, to access specific locations on DSK1 to
read/write(i.e
after creating an handle to it - which i am yet to figure out how),
could i use PhysicalAddressPointer(int,int) and then use
ReadByte/WriteByte in PhysicalAddressPointer class. Is this also an
alternative to DeviceIoControl and Createfile functions.
Thanks and Regards,
Rithesh
Student,
IIIT, Bangalore
<ctacke/> wrote:
You'd simply derive from StreamInterfaceDriver and set the prefix
to
DSK1:
and that gives you the foundation. Create a function for reading
and
have
it call its internal Read, and the same for Write.
For reading or writing any specific structure, you need to define
the
sructure (or class) and then pass it, following hte rules of CF
marshaling
appropriate for your version.
--
Chris Tacke
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--
Hello Chris,
From one of the previous discussions i understand that i could
as
well
use OpenNetCF to use createfile and deviceIocontrol in the
StreamInterfaceDriver. Kindly if u could throw some light on how
this
could be done, it would be great. I couldnt find much
documentation
for
this.
All i could know about this class was one could use this
abstract
base
class to create wrappers around Stream Interface Drivers that
are
not
supported by the CF. I am not able to figure out how i could
pass
SG_REQ structure.
It would help me a lot to use the functions OpenNetCF since then
i
wouldnt have to worry about deploying my project on phones with
different architectures. I am not an expert on .net CF, a
skeleton
flow
of how i could write this would be of great help.
Thanks and Regards,
Rithesh
Student,
IIIT, Bangalore
<ctacke/> wrote:
Well a char * would likely be a byte array, though your code is
not
conducive to that. You're using a strcpy on it, without
knowing a
length
which is generally very unsafe (this is how many security holes
in
the
desktop OS are created and exploited).
You code appears to assume ASCII string input. Also not the
best
design
for
CE - especially when you need to marshal from managed code
where
strings
are
always Unicode.
To keep with your definitions, change your P/Invoke to a
byte[],
then
take
your string data, pass it through Encoding.ASCII.GetBytes and
pass
that
through.
--
Chris Tacke
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--
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);...

read more »
 
G

Guest

Here's a better explanation:

http://blog.opennetcf.org/ctacke/PermaLink,guid,4677c4fd-008a-4394-b571-a14123ce67d3.aspx

-Chris


Hello Chris,
Just out of curiosity how do u actually send back a string from
a evc++ dll to the managed code.. as an example i was trying to use the
following code which i had given above.. but i am getting not getting
any thing back

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
extern "C" _declspec(dllexport) unsigned char* WINAPI Read()
{

unsigned char *lpOutBuf;
lpOutBuf = (unsigned char *)malloc(512);
memset(lpOutBuf, 0x97, 512);
return lpOutBuf;

}

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

and on the c# side i am importing as--

string t = Read();
..
..
..
..

[DllImport("SD_card_dll.dll",EntryPoint="Read")]
public static extern string Read();


The code runs without any exceptions but it only returns a null...

Whats wrong with the code.. or is there a better way to do it??


Thanks and Regards,
Rithesh
Student,
IIIT, Bangalore



Chris,
Thanks a lot for your help. I will keep you posted if this thing
actually works.. else may be i will have to use the DLL approach.

Thanks and regards,
Rithesh
Student,
IIIT, Bangalore

I've not used the DSK driver to do this, so I'm not sure if that is how
it
work. Intuitively that sounds right, but to know for sure I'd go look
in PB
for examples that do use the driver and see what's going on there, or
just
try that with known data to see if it works.
This is especially true for Flahs, as it has to be put into write mode
before a write occurs. Maybe the driver does that - maybe not. And if
it's
a TFAT system I'd be interested to see how that works. There are a
whole
lot of unknowns here without doing research, and all in all I can't see
what
good this is for anyway. For disk access there is a file system, and
that's
what you should be using. Doing direct reads and writes sounds like a
recipe for a corrupt file system to me.
--
Chris Tacke
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--
Wrecked said:
Chris,
After i pass the DSK1 to the ctor, let me know if this approach is
valid. To write/read to a specific location-
I open() it, then seek() to the required location and then use
read()/write() and then close() it.
Thanks and Regards
Rithesh
Student,
IIIT, Bangalore
<ctacke/> wrote:
If you look at the StreamInterfaceDriver class, it's ctor takes a
device
prefix, which is what it passes to CreateFile.
I'd discourage using the PhysicalAddressPointer for a few reasons:
1. it circumvents any existing driver, so you could get conflicts
2. it's not supported in CE 6.0
3. disks don't really work that way
--
Chris Tacke
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--
Hello Chris,
Thanks again for your help. I am still trying to hook up with
C#
and excuse me if u find my questions too simple. I couldnt exactly
follow - "set the prefix to DSK1:", kindly could u be a bit more
specific as how i could do that.
Staying with c#, to access specific locations on DSK1 to
read/write(i.e
after creating an handle to it - which i am yet to figure out how),
could i use PhysicalAddressPointer(int,int) and then use
ReadByte/WriteByte in PhysicalAddressPointer class. Is this also an
alternative to DeviceIoControl and Createfile functions.
Thanks and Regards,
Rithesh
Student,
IIIT, Bangalore
<ctacke/> wrote:
You'd simply derive from StreamInterfaceDriver and set the prefix
to
DSK1:
and that gives you the foundation. Create a function for reading
and
have
it call its internal Read, and the same for Write.
For reading or writing any specific structure, you need to define
the
sructure (or class) and then pass it, following hte rules of CF
marshaling
appropriate for your version.
--
Chris Tacke
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--
Hello Chris,
From one of the previous discussions i understand that i could
as
well
use OpenNetCF to use createfile and deviceIocontrol in the
StreamInterfaceDriver. Kindly if u could throw some light on how
this
could be done, it would be great. I couldnt find much
documentation
for
this.
All i could know about this class was one could use this
abstract
base
class to create wrappers around Stream Interface Drivers that
are
not
supported by the CF. I am not able to figure out how i could
pass
SG_REQ structure.
It would help me a lot to use the functions OpenNetCF since then
i
wouldnt have to worry about deploying my project on phones with
different architectures. I am not an expert on .net CF, a
skeleton
flow
of how i could write this would be of great help.
Thanks and Regards,
Rithesh
Student,
IIIT, Bangalore
<ctacke/> wrote:
Well a char * would likely be a byte array, though your code is
not
conducive to that. You're using a strcpy on it, without
knowing a
length
which is generally very unsafe (this is how many security holes
in
the
desktop OS are created and exploited).
You code appears to assume ASCII string input. Also not the
best
design
for
CE - especially when you need to marshal from managed code
where
strings
are
always Unicode.
To keep with your definitions, change your P/Invoke to a
byte[],
then
take
your string data, pass it through Encoding.ASCII.GetBytes and
pass
that
through.
--
Chris Tacke
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--
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);...

read more »
 
W

Wrecked

Hello Chris,

Thanks a lot for that posting.

Regards,

Rithesh
Student
IIIT, Bangalore

Here's a better explanation:

http://blog.opennetcf.org/ctacke/PermaLink,guid,4677c4fd-008a-4394-b571-a14123ce67d3.aspx

-Chris


Hello Chris,
Just out of curiosity how do u actually send back a string from
a evc++ dll to the managed code.. as an example i was trying to use the
following code which i had given above.. but i am getting not getting
any thing back

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
extern "C" _declspec(dllexport) unsigned char* WINAPI Read()
{

unsigned char *lpOutBuf;
lpOutBuf = (unsigned char *)malloc(512);
memset(lpOutBuf, 0x97, 512);
return lpOutBuf;

}

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

and on the c# side i am importing as--

string t = Read();
.
.
.
.

[DllImport("SD_card_dll.dll",EntryPoint="Read")]
public static extern string Read();


The code runs without any exceptions but it only returns a null...

Whats wrong with the code.. or is there a better way to do it??


Thanks and Regards,
Rithesh
Student,
IIIT, Bangalore



Chris,
Thanks a lot for your help. I will keep you posted if this thing
actually works.. else may be i will have to use the DLL approach.

Thanks and regards,
Rithesh
Student,
IIIT, Bangalore

I've not used the DSK driver to do this, so I'm not sure if that is how
it
work. Intuitively that sounds right, but to know for sure I'd go look
in PB
for examples that do use the driver and see what's going on there, or
just
try that with known data to see if it works.
This is especially true for Flahs, as it has to be put into write mode
before a write occurs. Maybe the driver does that - maybe not. And if
it's
a TFAT system I'd be interested to see how that works. There are a
whole
lot of unknowns here without doing research, and all in all I can't see
what
good this is for anyway. For disk access there is a file system, and
that's
what you should be using. Doing direct reads and writes sounds like a
recipe for a corrupt file system to me.
--
Chris Tacke
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--
Chris,
After i pass the DSK1 to the ctor, let me know if this approach is
valid. To write/read to a specific location-
I open() it, then seek() to the required location and then use
read()/write() and then close() it.
Thanks and Regards
Rithesh
Student,
IIIT, Bangalore
<ctacke/> wrote:
If you look at the StreamInterfaceDriver class, it's ctor takes a
device
prefix, which is what it passes to CreateFile.
I'd discourage using the PhysicalAddressPointer for a few reasons:
1. it circumvents any existing driver, so you could get conflicts
2. it's not supported in CE 6.0
3. disks don't really work that way
--
Chris Tacke
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--
Hello Chris,
Thanks again for your help. I am still trying to hook up with
C#
and excuse me if u find my questions too simple. I couldnt exactly
follow - "set the prefix to DSK1:", kindly could u be a bit more
specific as how i could do that.
Staying with c#, to access specific locations on DSK1 to
read/write(i.e
after creating an handle to it - which i am yet to figure out how),
could i use PhysicalAddressPointer(int,int) and then use
ReadByte/WriteByte in PhysicalAddressPointer class. Is this alsoan
alternative to DeviceIoControl and Createfile functions.
Thanks and Regards,
Rithesh
Student,
IIIT, Bangalore
<ctacke/> wrote:
You'd simply derive from StreamInterfaceDriver and set the prefix
to
DSK1:
and that gives you the foundation. Create a function for reading
and
have
it call its internal Read, and the same for Write.
For reading or writing any specific structure, you need to define
the
sructure (or class) and then pass it, following hte rules of CF
marshaling
appropriate for your version.
--
Chris Tacke
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--
Hello Chris,
From one of the previous discussions i understand that i could
as
well
use OpenNetCF to use createfile and deviceIocontrol in the
StreamInterfaceDriver. Kindly if u could throw some light on how
this
could be done, it would be great. I couldnt find much
documentation
for
this.
All i could know about this class was one could use this
abstract
base
class to create wrappers around Stream Interface Drivers that
are
not
supported by the CF. I am not able to figure out how i could
pass
SG_REQ structure.
It would help me a lot to use the functions OpenNetCF since then
i
wouldnt have to worry about deploying my project on phones with
different architectures. I am not an expert on .net CF, a
skeleton
flow
of how i could write this would be of great help.
Thanks and Regards,
Rithesh
Student,
IIIT, Bangalore
<ctacke/> wrote:
Well a char * would likely be a byte array, though your codeis
not
conducive to that. You're using a strcpy on it, without
knowing a
length
which is generally very unsafe (this is how many security holes
in
the
desktop OS are created and exploited).
You code appears to assume ASCII string input. Also not the
best
design
for
CE - especially when you need to marshal from managed code
where
strings
are
always Unicode.
To keep with your definitions, change your P/Invoke to a
byte[],
then
take
your string data, pass it through Encoding.ASCII.GetBytes and
pass
that
through.
--
Chris Tacke
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--
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);...

read more »
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top