473,320 Members | 1,865 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

C# marshal string PTCHAR TCHAR* help needed

Mo
Hi, i am trying to call A dll FUNCTION from a C# application.
i am using this function

int hhpGetErrorMessage(PTCHAR ptcErrorMsg,int nMaxChars)
where
ptcErrMsg: TCHAR buffer to hold error message string.
nMaxChars: Maximum number of characters that can fit in ptcErrorMsg
including NULL.

i defined this function in C# as
// Miscellaneous Error Code Message function.
[DllImport("hhpImgrSdk.dll", CharSet=CharSet.Auto)]
public static extern int hhpGetErrorMessage(string ptcErrorMsg,int
nMaxChars );

i call this function as

string s();
hhpGetErrorMessage(s,128);

i get an error message " Use of unassigned local variable 's'"

when i declare s as string s =new string (' ',128);

it seems to pass the error msg, but the result i get from s is junk
a junk value such as "œ—ž¾'Ì•Š"¬⁤ œäûr"

i know ptcErrorMsg will hold the error msg returned by
hhpGetErrorMessage()
when i tried to change my function to accept a ref to a string such as
[DllImport("hhpImgrSdk.dll", CharSet=CharSet.Auto)]
public static extern int hhpGetErrorMessage(ref string ptcErrorMsg,int
nMaxChars );

i got an Exception "An unhandled exception of type
'System.ExecutionEngineException'"

i dont know for sure if its a Marshaling problem?
i know that PTCHAR,TCHAR* can be substituted in C# by string..
i cannot change the DLL !!
how do i declare a string of a certain size without an initial value

any idea?

thanks all
Mo
ma******@hotmail.com
Nov 16 '05 #1
2 10398
Mo,

You want to make the declaration like this:

[DllImport("hhpImgrSdk.dll", CharSet=CharSet.Auto)]
public static extern int hhpGetErrorMessage(StringBuilder ptcErrorMsg, int
nMaxChars);

And then call it like this:

// Allocate a StringBuilder.
StringBuilder pobjErrorMsg = new StringBuilder(128);

// Now make the call.
hhpGetErrorMessage(pobjErrorMsg, pobjErrorMsg.Capacity);

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Mo" <ma******@hotmail.com> wrote in message
news:78*************************@posting.google.co m...
Hi, i am trying to call A dll FUNCTION from a C# application.
i am using this function

int hhpGetErrorMessage(PTCHAR ptcErrorMsg,int nMaxChars)
where
ptcErrMsg: TCHAR buffer to hold error message string.
nMaxChars: Maximum number of characters that can fit in ptcErrorMsg
including NULL.

i defined this function in C# as
// Miscellaneous Error Code Message function.
[DllImport("hhpImgrSdk.dll", CharSet=CharSet.Auto)]
public static extern int hhpGetErrorMessage(string ptcErrorMsg,int
nMaxChars );

i call this function as

string s();
hhpGetErrorMessage(s,128);

i get an error message " Use of unassigned local variable 's'"

when i declare s as string s =new string (' ',128);

it seems to pass the error msg, but the result i get from s is junk
a junk value such as "o-z¾'Ì.S"¬⁤ oäûr"

i know ptcErrorMsg will hold the error msg returned by
hhpGetErrorMessage()
when i tried to change my function to accept a ref to a string such as
[DllImport("hhpImgrSdk.dll", CharSet=CharSet.Auto)]
public static extern int hhpGetErrorMessage(ref string ptcErrorMsg,int
nMaxChars );

i got an Exception "An unhandled exception of type
'System.ExecutionEngineException'"

i dont know for sure if its a Marshaling problem?
i know that PTCHAR,TCHAR* can be substituted in C# by string..
i cannot change the DLL !!
how do i declare a string of a certain size without an initial value

any idea?

thanks all
Mo
ma******@hotmail.com

Nov 16 '05 #2


Hi Nicholas,,

That worked great,, but i had to change CharSet to CharSet.Ansi, cause
CharSet.Auto

gave me some junk..

i really appreciate it.

take care

Mo

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Krzysztof | last post by:
I must use methods from standard dll im my C#code. In dll declaration of method is like this: typedef (__stdcall *ExecuteTaskFromXML)( const string& input, string& output, DWORD& dwSize, LPVOID&...
2
by: Mo | last post by:
Hi, i am trying to call unmanaged C++ DLL in my C# application i converted all the structures to be .net compatible (e.g TCHAR to string (including Marshal As) one of the functions i try to...
0
by: Mo | last post by:
I am having problem with marshaling struct in C#. //the original C++ struct typedef struct _tagHHP_DECODE_MSG { DWORD dwStructSize; // Size of decode structure. TCHAR ...
7
by: Mo | last post by:
I am having problem with marshaling struct in C#. //the original C++ struct typedef struct _tagHHP_DECODE_MSG { DWORD dwStructSize; // Size of decode structure. TCHAR ...
2
by: zDog | last post by:
I found a lot of information on passing data from C# to a C++ dll What I cannot find is a way to return C++ structs of TCHAR string data back to the C# managed code! typedef struct // C++ data...
8
by: Duncan Winn | last post by:
I am new to VC++7. I am using a method GetPrivateProfileString that requires an LPTSTR. I have defined this as a: char * data_name; I am then trying to convert this to an LPOLESTR and I...
1
by: Goran | last post by:
Hi all! I need to pass managed String from to C-style APIs. I see I can use Marshal::StringToXXX functions. Is this the best we have? I understand this will allocate a new string and create copy...
10
by: Lucy Ludmiller | last post by:
Anyone knows how to convert a LPCTSTR to an STL striung?. Can't seem to finda nyting (that dosen't blab on for several pages) on the net about how to do this
14
by: =?Utf-8?B?Sm9hY2hpbQ==?= | last post by:
I have seen the following function to convert from a System::String^ to a const wchar_t*. I would like to get a LPCTSTR and AFAIK LPCTSTR is equal to const wchar_t*. Then it should all work right?...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.