Click here to Skip to main content
16,014,952 members
Home / Discussions / C#
   

C#

 
QuestionPopulating a ComboBox Pin
Civic0610-Dec-06 5:33
Civic0610-Dec-06 5:33 
AnswerRe: Populating a ComboBox Pin
teejayem10-Dec-06 5:48
teejayem10-Dec-06 5:48 
AnswerRe: Populating a ComboBox Pin
Robert Rohde10-Dec-06 5:53
Robert Rohde10-Dec-06 5:53 
AnswerRe: Populating a ComboBox Pin
Luis Alonso Ramos10-Dec-06 7:15
Luis Alonso Ramos10-Dec-06 7:15 
Questiontelephonic application Pin
amgag10-Dec-06 5:04
amgag10-Dec-06 5:04 
QuestionHow to search/replace words in a pre-formatted pdf file Pin
babbelfisken10-Dec-06 3:59
babbelfisken10-Dec-06 3:59 
Questionsend data over modem Pin
sianatia10-Dec-06 1:55
sianatia10-Dec-06 1:55 
QuestionMarshaling a string to a native dll... Pin
Shy Agam10-Dec-06 1:16
Shy Agam10-Dec-06 1:16 
Hello,

I wrote a native C++ dll.
One of the functions it exports receives a string (LPCSTR to be exact), and keeps this string in order to use it later.
When my C# app simply sends a string to the dll, the GC is collecting it as soon as the function returns.

Problem is, I need that string in other functions.
So I've tried using GCHandle like so:

Global stuff:
string s = "MyVeryImportantString";
GCHandle gch;
Inside a C# function:
gch = GCHandle.Alloc(s, GCHandleType.Pinned);
nativeDllFunction(GCHandle.ToIntPtr(gch));
Later on I free the GCHandle of course.

That didn't work, but that's probably because I'm doing something wrong, and it probably has a solution...
That's not why I'm writing right now...
I'm here to ask your opinions about my workaround...
Inside my native dll, I wrote a function to allocate new memory for the string, using malloc.
This way I'm not holding a pointer to the string the GC is collecting, but a pointer to a copy I made.

Doing so could help future mistakes with managed assemblies which use this dll.
Moreover, it saves me the need to worry, and the time to write a proper GCHandle code.
As I'm new to dll programming in C++, I don't really know fashionable methods of doing stuff. Is it bad to use the malloc function inside of my dll?
Is it better to use the GCHandle method instead?

Thanks in advance,
Shy.
AnswerRe: Marshaling a string to a native dll... Pin
Luc Pattyn10-Dec-06 1:49
sitebuilderLuc Pattyn10-Dec-06 1:49 
QuestionHow can I implement "OLE object" with C# and SQL Pin
devloperx10-Dec-06 0:09
devloperx10-Dec-06 0:09 
AnswerRe: How can I implement "OLE object" with C# and SQL Pin
devloperx10-Dec-06 2:21
devloperx10-Dec-06 2:21 
QuestionWorkgroup name of local host Pin
cashi_qau9-Dec-06 23:13
cashi_qau9-Dec-06 23:13 
GeneralRe: Workgroup name of local host Pin
gnadeem10-Dec-06 3:10
gnadeem10-Dec-06 3:10 
QuestionString - ValueType or ReferenceType? Pin
Shy Agam9-Dec-06 22:43
Shy Agam9-Dec-06 22:43 
AnswerRe: String - ValueType or ReferenceType? Pin
Christian Graus9-Dec-06 23:23
protectorChristian Graus9-Dec-06 23:23 
GeneralRe: String - ValueType or ReferenceType? Pin
Shy Agam9-Dec-06 23:26
Shy Agam9-Dec-06 23:26 
GeneralRe: String - ValueType or ReferenceType? Pin
Christian Graus9-Dec-06 23:30
protectorChristian Graus9-Dec-06 23:30 
GeneralRe: String - ValueType or ReferenceType? Pin
Shy Agam9-Dec-06 23:30
Shy Agam9-Dec-06 23:30 
AnswerRe: String - ValueType or ReferenceType? Pin
Robert Rohde9-Dec-06 23:25
Robert Rohde9-Dec-06 23:25 
GeneralRe: String - ValueType or ReferenceType? Pin
Shy Agam9-Dec-06 23:28
Shy Agam9-Dec-06 23:28 
GeneralRe: String - ValueType or ReferenceType? Pin
Christian Graus9-Dec-06 23:49
protectorChristian Graus9-Dec-06 23:49 
GeneralRe: String - ValueType or ReferenceType? Pin
Guffa10-Dec-06 2:21
Guffa10-Dec-06 2:21 
GeneralRe: String - ValueType or ReferenceType? Pin
George L. Jackson12-Dec-06 0:19
George L. Jackson12-Dec-06 0:19 
GeneralRe: String - ValueType or ReferenceType? Pin
Guffa12-Dec-06 15:04
Guffa12-Dec-06 15:04 
GeneralRe: String - ValueType or ReferenceType? Pin
George L. Jackson12-Dec-06 15:29
George L. Jackson12-Dec-06 15:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.