By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,333 Members | 1,381 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,333 IT Pros & Developers. It's quick & easy.

Pinvoke reading files, returning char * values.

P: n/a
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

Apr 4 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"anirudh" <an*********@gmail.com> wrote in message
news:11**********************@z34g2000cwc.googlegr oups.com...
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

Apr 4 '06 #2

P: n/a
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

Apr 4 '06 #3

P: n/a
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

Apr 5 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.