Hi Peter,
I could open a file and write to it successfully.
But I am having trouble in reading from the file.
I am using "coredll.dll" and .NET Compact Framework and C#.
This is how I am trying to read from a file.
[DllImport("coredll.dll")]
public static extern char[] fgets( char[] buffer, int max_length, IntPtr
fPtr);
[DllImport("coredll.dll")]
public static extern IntPtr fopen( byte[] fname, byte[] fmode);
[DllImport("coredll.dll")]
public static extern int fclose(IntPtr fname);
// I am converting a file name string in to unicode byte array.
private byte[] getByteArray(string str, ASCIIEncoding ae)
{
byte[] result = null;
int str_size = str.Length;
result = new byte[str_size];
if( (ae.GetBytes(str,0,str_size,result,0)== 0 ) )
{
result = null;
}
return result;
}
now I am calling the methods in this way.
byte[] fname = getByteArray("fileName", ae);
byte[] mode = getByteArray("r",ae);
IntPtr fptr = fopen(fname,mode);
char[] buffer;
fgets( buffer,255,fptr);
Then I am getting "NotSupportedException".
Could you please let me know where I went wrong.
I am opening the file properly. Is there any better way of opening
a file, convering a string to unicode string.
Cheers,
Naveen.
Peter Foot said:
Your second parameter should be a string so change your code to:-
[DllImport("coredll")]
static extern IntPtr fopen(string pchFileName, string pchAccessMode);
Peter
--
Peter Foot
Windows Embedded MVP
www.inthehand.com |
www.opennetcf.org
Do have an opinion on the effectiveness of Microsoft Windows Mobile and
Embedded newsgroups? Let us know!
https://www.windowsembeddedeval.com/community/newsgroups
Naveen Mukkelli said:
Hi Neil,
Thanks for the reply. I'm having problems in opening the file.
when I tried using
[DllImport("coredll")]
static extern IntPtr fopen(string pchFileName, pchAccessMode);
I am getting "A native exception has ouccurred in xxx.exe"
ExceptionCode: 0xc0000005
ExceptionAddress: 0x013a1350
Reading: 0x2a000300
Could you please explain what exactly is happening here.
Cheers,
Naveen.
Neil Cowburn said:
*ANY* pointers that you need to marshal across the interop boundary can
be
send as IntPtr.
In this case, your method P/Invoke signature would be:
[DllImport("somedll")]
public static extern int SomeMethod(IntPtr fptr, uint len);
You can P/Invoke fopen in coredll to get the FILE*,e.g.
[DllImport("coredll")]
static extern IntPtr fopen(string pchFileName, pchAccessMode);
IntPtr fptr = fopen("\\My Documents\\SomeFile.ext","r");
HTH
Neil
message Hi,
Can we send File pointers from managed code to a method in unmanaged
code
at all ?
One of the methods in the DLL I am using requires a file pointer as its
arguments.
The signature of the method in the DLL looks like this,
int SomeMethod( File *fptr, uint len ); ( C method )
How can we call this method from C#,
I mean,
[DllImport (" someDll.dll", CharSet = CharSet.Auto)]
public static extern int SomeMethod( ?, uint len );
Kindly let me know,
Cheers,
Naveen.