473,408 Members | 1,854 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,408 software developers and data experts.

using pinvoke to fill a string

I have a 3rd party unmanged .dll. This has functions that will look up
information and fill a C string passed to it with data. I want to use this
data in .NET classes after it has been filled and I am programming in C++. I
believe that I should use pinvoke, but I see a problem that I can't figure
how to get around. I don't think I can declare the string I want as a String
class since when I pass it to unmanged function it is not yet allocated and
the unmanged function does not allocate space it just fills it. So I would
think this would cause memory corruption. I was also thinking that I could
create the String class and fill it with blanks so memory was allocated, but
I don't know if the marshling will correctly change value since String class
are basically a static value unless you use correct member function that
deletes old value and then adds new value.

If any one can tell me the correct way to set this up it would be very much
appreciated.
Thanks
Nov 23 '05 #1
2 1872
JAL
Brian.. In C++/cli given"

bool Copy(char* out,int size,const char * in) {
strcpy_s(out,size,in);
bool isNullTerminated= false;
if (in[size-1] == '\0')
isNullTerminated= true;
return isNullTerminated;
}

You can do:

// exercise String to Ansi Ansi to String
String^ Echo(String^ in) {
NativeChild nc;
IntPtr ip(0);
String^ out=L"";
char* str= 0;
try {
str= new char[in->Length+1]; // null terminator
ip = Marshal::StringToHGlobalAnsi(in); // create char[] on unmanaged heap
nc.Copy(str,in->Length+1,static_cast<const char*>(ip.ToPointer()));
out= Marshal::PtrToStringAnsi(static_cast<IntPtr>(str)) ;
}
catch(...) {
out= L"exception thrown";
}
finally {
if (ip != IntPtr(0)) {
Marshal::FreeHGlobal(ip); // release char[] on unmanaged heap
}
if(str) {
delete [] str; // release char[] on unmanaged heap
}
}
return out;
}

:)

"brian_harris" wrote:
I have a 3rd party unmanged .dll. This has functions that will look up
information and fill a C string passed to it with data. I want to use this
data in .NET classes after it has been filled and I am programming in C++. I
believe that I should use pinvoke, but I see a problem that I can't figure
how to get around. I don't think I can declare the string I want as a String
class since when I pass it to unmanged function it is not yet allocated and
the unmanged function does not allocate space it just fills it. So I would
think this would cause memory corruption. I was also thinking that I could
create the String class and fill it with blanks so memory was allocated, but
I don't know if the marshling will correctly change value since String class
are basically a static value unless you use correct member function that
deletes old value and then adds new value.

If any one can tell me the correct way to set this up it would be very much
appreciated.
Thanks

Nov 23 '05 #2
Thanks, that got me a bit further. If you don't mind I do have a couple of
questions about your syntax.
You use the declaration of a string as String^ not String * which is what I
had to use to get it to work.
Also in your intilization of out you use L"" , I have seen programs using
S"some string", but not L. I have not been able to find any documentation on
what these stand for or when you need to use them. If you could tell me
where to find information on this it would be appreciated. I presume that
there are also other prefixes that have some other meanings.

"JAL" wrote:
Brian.. In C++/cli given"

bool Copy(char* out,int size,const char * in) {
strcpy_s(out,size,in);
bool isNullTerminated= false;
if (in[size-1] == '\0')
isNullTerminated= true;
return isNullTerminated;
}

You can do:

// exercise String to Ansi Ansi to String
String^ Echo(String^ in) {
NativeChild nc;
IntPtr ip(0);
String^ out=L"";
char* str= 0;
try {
str= new char[in->Length+1]; // null terminator
ip = Marshal::StringToHGlobalAnsi(in); // create char[] on unmanaged heap
nc.Copy(str,in->Length+1,static_cast<const char*>(ip.ToPointer()));
out= Marshal::PtrToStringAnsi(static_cast<IntPtr>(str)) ;
}
catch(...) {
out= L"exception thrown";
}
finally {
if (ip != IntPtr(0)) {
Marshal::FreeHGlobal(ip); // release char[] on unmanaged heap
}
if(str) {
delete [] str; // release char[] on unmanaged heap
}
}
return out;
}

:)

"brian_harris" wrote:
I have a 3rd party unmanged .dll. This has functions that will look up
information and fill a C string passed to it with data. I want to use this
data in .NET classes after it has been filled and I am programming in C++. I
believe that I should use pinvoke, but I see a problem that I can't figure
how to get around. I don't think I can declare the string I want as a String
class since when I pass it to unmanged function it is not yet allocated and
the unmanged function does not allocate space it just fills it. So I would
think this would cause memory corruption. I was also thinking that I could
create the String class and fill it with blanks so memory was allocated, but
I don't know if the marshling will correctly change value since String class
are basically a static value unless you use correct member function that
deletes old value and then adds new value.

If any one can tell me the correct way to set this up it would be very much
appreciated.
Thanks

Nov 29 '05 #3

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

Similar topics

1
by: Muthiah Samy | last post by:
Hi All, I am trying to call COM Component in .NET using PInvoke. In the following scenario, ( I am getting an exception called SEH - External component has thrown an exception) UnManaged...
2
by: John Smith | last post by:
Hello :) I have a DLL file written in C where one function accepts an array of structures as input paramter and pointer to length integer. This function will fill up the array and specify the...
2
by: Bob Dankert | last post by:
I need to use functions through PInvoke which accept and return pointers to strings. What is the best way to get string pointers (IntPtr I am guessing?) to pass into these functions, and the best...
3
by: Charles Denny | last post by:
I'm trying to call CertFindCertificateInStore to find all certificates in the store that have the Code Signing enhanced key usage. I'm running into problems marshalling the array of OIDs in...
1
by: Marek | last post by:
Hi I need to call a native function in a C++ dll from my C#. The C++ function declaration is shown below. The C# code that I have been trying to perform the call is also shown below. All it is...
7
by: Rymfax | last post by:
I would really appreciate it if someone could help me figure out what I'm doing wrong trying to PInvoke SetupDiEnumDriverInfo. All the other PInvokes i've done up to this point work fine. Whenver...
0
by: gregarican | last post by:
A couple of days ago I was debating about whether to use C++/CLI or C# 2005's PInvoke in order to tie in my VS 2005 app with a legacy csta32.dll file. So far I have tried to call one method out of...
14
by: Mohamed Mansour | last post by:
Hey there, this will be somewhat a long post, but any response is appreciated! I have done many PInvoke in the past from C++ to C#, but I did PInvoke within C# not C++/CLI. Can someone explain...
0
by: emitojleyes | last post by:
Hi everyone! i'm new at this forum as well as programming in .net I have to develop a module that can use and configure a fingerprint detector; its documentation is written in C, as you will see...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.