Pinvoke reading files, returning char * values.

A

anirudh

Hello,
I am trying to use an Win32 DLL developed using Visual C++ 6.0 in my
C# .net application.

The signature of the original function is as follows :

BOOL __stdcall GetXMLString(
const char* strInputFileName,
const char* strPassword,
char* strOutput,
char* strErrorMessage)


So, this function takes the path of a file and a password and returns
back a string.


I translated like this :


[DllImport("STLKit.dll", CharSet=CharSet.Auto )]
public static extern bool GetXMLString(string inputFileName,
string
password,
ref
StringBuilder output,
string
errorMessage);


However this is not working at all and the output parameter is being
populated by weird messages like "publictoken" or "Dependancy
Evaluated" and so on.

My questions are :

Are these the correct mappings ?
Could there be a problem sending a file path to the function ?


Any help/pointers would really be very appreciated.


Thanks,
Anirudh
 
N

Nicholas Paldino [.NET/C# MVP]

anirudh,

The signature should be as follows:

[DllImport("STLKit.dll", CharSet=CharSet.Ansi )]
public static extern bool GetXMLString(
string inputFileName,
string password,
StringBuilder output,
StringBuilder errorMessage);

However, you need to make sure that you set the capacity of the
StringBuilder such that the memory buffer has enough to be written to before
you call the function. The problem that I see with it is that you have no
parameters to indicate how much is going to be written in the output and
errorMessage parameters, and you can possibly write past the buffer passed
in.

Hope this helps.
 
A

anirudh

Nicholas,
Really appreciate you taking time to answer my question.

In this case, I do know how much memory the output and the error
Message could possibly have, so I changed it to the function signature
you provided and in the code
allocated a capacity of the StringBuilder parameters to that.

I was however getting access violation errors.
However if I added the attribute, MarshalAs(UnmanagedType.LPTStr) then
the access violation errors went away,

[DllImport("C:\\STLKit.dll", CharSet = CharSet.Ansi) ]
public static extern bool
GetXMLString([In][MarshalAs(UnmanagedType.LPTStr)]string
strInputFileName,

[In][MarshalAs(UnmanagedType.LPTStr)]string strPassword,
StringBuilder strOutput,
StringBuilder
strErrorMessage);

Looks like now, the return parameters are working fine, however the
input parameters are not working.

Do you have any suggestions on what could be causing the problems, what
else could I try for the input parameters ?

Thanks once again,

Anirudh
 
A

anirudh

Hello,
I have got a little further ahead.

This is the original function signature.

BOOL __stdcall GetXMLString(
const char* strInputFileName,
const char* strPassword,
char* strOutput,
char* strErrorMessage)


This is the signature i am using :

[DllImport("C:\\STLKit.dll", CharSet = CharSet.Ansi)]
public static extern bool GetXMLString(
[In][MarshalAs(UnmanagedType.LPTStr)]string
strInputFileName,
[In][MarshalAs(UnmanagedType.LPTStr)]string strPassword,
StringBuilder strOutput,
StringBuilder strErrorMessage);

The problem is that ONLY the first character of the string
strInputFileName is being passed to the function.
Do I need to allocate memory for the strInputFileName ?

Any response would be highly appreciated.

Thanks,
Anirudh
 

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