Re: Help for Marshalling by Wrecked
Wrecked
Mon Dec 18 10:20:58 CST 2006
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
> --
>
>
>
> "Wrecked" <rithesh.rg@gmail.com> wrote in message
> news:1166457040.808907.41090@79g2000cws.googlegroups.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
> >> --
> >>
> >>
> >> "Wrecked" <rithesh.rg@gmail.com> wrote in message
> >> news:1166453269.094586.65330@80g2000cwy.googlegroups.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
> >> >> --
> >> >>
> >> >>
> >> >>
> >> >> "Wrecked" <rithesh.rg@gmail.com> wrote in message
> >> >> news:1166417850.957390.142050@79g2000cws.googlegroups.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
> >> >> >
> >> >
> >