How to handle File Pointers (*File) from C++ dll?

G

Guest

Hello,

I am writing a class in C# that uses [DllImport("dllname.dll")] to call
functions from a C++ dll. Evenything seems to work fine call the functions
except one of the functions takes a *File as a parameter.

Many of the functions also used char* and int* and I used string and IntPtr
respectively. Does anyone know a way to handle the file pointers?

This is the C++ declaration:
CPP DEBUG_API int interpret_command(char *inString, FILE *inputFile);

And I want to use:
[DllImport("debug.dll")]
static extern int interpret_command(string command, ??What goes here??);

Thanks in advance for any help.

Wes
 
S

Sean Hederman

Wes said:
Hello,

I am writing a class in C# that uses [DllImport("dllname.dll")] to call
functions from a C++ dll. Evenything seems to work fine call the
functions
except one of the functions takes a *File as a parameter.

Many of the functions also used char* and int* and I used string and
IntPtr
respectively. Does anyone know a way to handle the file pointers?

This is the C++ declaration:
CPP DEBUG_API int interpret_command(char *inString, FILE *inputFile);

And I want to use:
[DllImport("debug.dll")]
static extern int interpret_command(string command, ??What goes here??);

[DllImport("debug.dll")]
static extern int interpret_command(string command, IntPtr inputFile);
 
G

Guest

Hey Sean,

Thanks for the post. I tried using the IntPtr and I get an "Object
reference not set to an instance of an object." exception.

Here is what I tried.

[DllImport("debug.dll")]
static extern int interpret_command(string command, IntPtr file);

public void SomeFunction()
{
IntPtr filePointer = new IntPtr();
try
{
status = interpret_command("test command", filePointer);
}
catch(Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}

Does this seem right to you??

Thanks,
Wes

Sean Hederman said:
Wes said:
Hello,

I am writing a class in C# that uses [DllImport("dllname.dll")] to call
functions from a C++ dll. Evenything seems to work fine call the
functions
except one of the functions takes a *File as a parameter.

Many of the functions also used char* and int* and I used string and
IntPtr
respectively. Does anyone know a way to handle the file pointers?

This is the C++ declaration:
CPP DEBUG_API int interpret_command(char *inString, FILE *inputFile);

And I want to use:
[DllImport("debug.dll")]
static extern int interpret_command(string command, ??What goes here??);

[DllImport("debug.dll")]
static extern int interpret_command(string command, IntPtr inputFile);
Thanks in advance for any help.

Wes
 
J

John Puopolo

Hi....

Try a ref. If memory serves, a FILE* is nothing more than a long* - so you
need a pointer (a ref in C#).

John Puopolo
 
G

Guest

Hi John,

Thanks for the post.

This is what I tried after getting your post, and I still the error "Object
reference not set to an instance of an object."

[DllImport("debug.dll")]
static extern int interpret_command(string command, ref int file);

public void SomeFunction()
{
int filePointer = 0;

try
{
status = interpret_command("test command",ref filePointer);
}
catch(Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}

Can you see a problem with this??

John Puopolo said:
Hi....

Try a ref. If memory serves, a FILE* is nothing more than a long* - so you
need a pointer (a ref in C#).

John Puopolo


Wes said:
Hello,

I am writing a class in C# that uses [DllImport("dllname.dll")] to call
functions from a C++ dll. Evenything seems to work fine call the functions
except one of the functions takes a *File as a parameter.

Many of the functions also used char* and int* and I used string and IntPtr
respectively. Does anyone know a way to handle the file pointers?

This is the C++ declaration:
CPP DEBUG_API int interpret_command(char *inString, FILE *inputFile);

And I want to use:
[DllImport("debug.dll")]
static extern int interpret_command(string command, ??What goes here??);

Thanks in advance for any help.

Wes
 
S

Sean Hederman

I just assumed that FILE was a file handle. Is it maybe a struct or
something?

Wes said:
Hey Sean,

Thanks for the post. I tried using the IntPtr and I get an "Object
reference not set to an instance of an object." exception.

Here is what I tried.

[DllImport("debug.dll")]
static extern int interpret_command(string command, IntPtr file);

public void SomeFunction()
{
IntPtr filePointer = new IntPtr();
try
{
status = interpret_command("test command", filePointer);
}
catch(Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}

Does this seem right to you??

Thanks,
Wes

Sean Hederman said:
Wes said:
Hello,

I am writing a class in C# that uses [DllImport("dllname.dll")] to call
functions from a C++ dll. Evenything seems to work fine call the
functions
except one of the functions takes a *File as a parameter.

Many of the functions also used char* and int* and I used string and
IntPtr
respectively. Does anyone know a way to handle the file pointers?

This is the C++ declaration:
CPP DEBUG_API int interpret_command(char *inString, FILE *inputFile);

And I want to use:
[DllImport("debug.dll")]
static extern int interpret_command(string command, ??What goes
here??);

[DllImport("debug.dll")]
static extern int interpret_command(string command, IntPtr inputFile);
Thanks in advance for any help.

Wes
 
G

Guest

I assume that the function you are calling is expecting a valid file handle
to be passed in. You are passing in IntPtr.Zero which is the equivalent of a
null pointer. try to open a filestream and pass in the FileStream.Handle as
the argument.

Wes said:
Hey Sean,

Thanks for the post. I tried using the IntPtr and I get an "Object
reference not set to an instance of an object." exception.

Here is what I tried.

[DllImport("debug.dll")]
static extern int interpret_command(string command, IntPtr file);

public void SomeFunction()
{
IntPtr filePointer = new IntPtr();
try
{
status = interpret_command("test command", filePointer);
}
catch(Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}

Does this seem right to you??

Thanks,
Wes

Sean Hederman said:
Wes said:
Hello,

I am writing a class in C# that uses [DllImport("dllname.dll")] to call
functions from a C++ dll. Evenything seems to work fine call the
functions
except one of the functions takes a *File as a parameter.

Many of the functions also used char* and int* and I used string and
IntPtr
respectively. Does anyone know a way to handle the file pointers?

This is the C++ declaration:
CPP DEBUG_API int interpret_command(char *inString, FILE *inputFile);

And I want to use:
[DllImport("debug.dll")]
static extern int interpret_command(string command, ??What goes here??);

[DllImport("debug.dll")]
static extern int interpret_command(string command, IntPtr inputFile);
Thanks in advance for any help.

Wes
 

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