File pointers ot unmanaged code ??

G

Guest

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.
 
N

Neil Cowburn

*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
 
C

Chris Tacke, eMVP

*But* the pointer must be valid and have the same meaning in both spaces.
You can pass a, unmanaged FILE * as an IntPtr, but if you're asking if you
can pass a System.IO.File to unmanaged code, then I'm not nearly as inclined
to say "yes." Where is the handle coming from in the managed code? Another
P/Invoke?

-Chris


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

Naveen Mukkelli said:
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.
 
N

Neil Cowburn

I concur wholeheartedly. That's why I included the snippet about getting the
FILE * through P/Invoke. The IntPtr *absolutely, must, without a doubt* be a
FILE * for it to work.


Chris Tacke said:
*But* the pointer must be valid and have the same meaning in both spaces.
You can pass a, unmanaged FILE * as an IntPtr, but if you're asking if you
can pass a System.IO.File to unmanaged code, then I'm not nearly as
inclined
to say "yes." Where is the handle coming from in the managed code?
Another
P/Invoke?

-Chris


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

Naveen Mukkelli said:
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.
 
G

Guest

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

Naveen Mukkelli said:
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.
 
P

Peter Foot [MVP]

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

Naveen Mukkelli said:
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.
 
G

Guest

Hi Peter,

I did declare as you have mentioned!!

I am using the declreration as follows..
IntPtr fptr = IntPtr.Zero;
fptr = fopen("\\My Documents\\ErrorLog.txt","w");

It is working when I tried with "msvcr71.dll" on the PC.

did I forget anything else? Kindly let me know.

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.
 
G

Guest

Hi Peter,

It seems that this is working in .NET Framework.
but It is not working in .NET Compact Framework.
my application will run on a PDA.

Could you suggest me how can I open a file in a PDA.

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.
 
G

Guest

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.
 

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