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

Using C DLL allocated memory in C# then destroying it.

I'm calling a function from a C DLL in .NET(C#).

The func. seems so:

void Sample(char *s)
{
s= calloc(6,1);
strcpy(s, "hello");
}

When I declare this function in C# I use the following notation:
[DllImport("sample.dll", CharSet = CharSet.Ansi)]
public static extern void Sample(out StringBuilder s);

However when I look at the watch window when debugging I can't see s =
"hello". Instead s = null;
And then after using this variable I want to destroy the memory
allocated with calloc in C# side.

Can you help me to solve this problem.
Nov 16 '05 #1
4 3257
I believe the problem you're having is because of teh Stringbuilder not
being initialized. You would need to create a StringBuilder oject and
pass it by value and it should work just fine.
HTH

H?seyin wrote:
I'm calling a function from a C DLL in .NET(C#).

The func. seems so:

void Sample(char *s)
{
s= calloc(6,1);
strcpy(s, "hello");
}

When I declare this function in C# I use the following notation:
[DllImport("sample.dll", CharSet = CharSet.Ansi)]
public static extern void Sample(out StringBuilder s);

However when I look at the watch window when debugging I can't see s =
"hello". Instead s = null;
And then after using this variable I want to destroy the memory
allocated with calloc in C# side.

Can you help me to solve this problem.


--
Regards,
Dilip Krishnan
MCAD, MCSD.net
dilipdotnet at apdiya dot com
Nov 16 '05 #2
void Sample(char *s)
{
s= calloc(6,1);
strcpy(s, "hello");
}
If you want to return the string pointer you must add another level of
indirection

void Sample(char **s)
{
*s = (char*)calloc(6,1);
strcpy(*s, "hello");
}

And then after using this variable I want to destroy the memory
allocated with calloc in C# side.


You can't do that, you must free the memory in the C DLL.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #3
Thanks for help.
I have added another level of indirection to the function char *s->
char **s.
So I have been able to get the value "hello" from C DLL.

Why can't I free the memory in C#. I saw that C# has some functions
like Marshall.FreeHGlobal. Can they help me to free the memory in C#
side?

Regards,
Hüseyin Özkan

Mattias Sjögren <ma********************@mvps.org> wrote in message news:<es**************@TK2MSFTNGP11.phx.gbl>...
void Sample(char *s)
{
s= calloc(6,1);
strcpy(s, "hello");
}


If you want to return the string pointer you must add another level of
indirection

void Sample(char **s)
{
*s = (char*)calloc(6,1);
strcpy(*s, "hello");
}

And then after using this variable I want to destroy the memory
allocated with calloc in C# side.


You can't do that, you must free the memory in the C DLL.

Mattias

Nov 16 '05 #4
Why can't I free the memory in C#. I saw that C# has some functions
like Marshall.FreeHGlobal. Can they help me to free the memory in C#
side?


No, FreeHGlobal should only be used to free memory allocated with
LocalAlloc/GlobalAlloc. To free memory allocated with the CRT memory
functions you should use free() from the same CRT, which in general
isn't directly accessible from C#. You could of course export a
cleanup function from your DLL that simply wraps a call to free. Or
you could change allocation API in your Sample function and use for
example LocalAlloc instead.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #5

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

Similar topics

10
by: eyh5 | last post by:
Hi, My C code (running on Soalris Unix) has some "segmentation fault" that I wish to use purify to do it. I poked around the web, and found some information about adding some lines in a Makefile...
4
by: H?seyin | last post by:
I'm calling a function from a C DLL in .NET(C#). The func. seems so: void Sample(char *s) { s= calloc(6,1); strcpy(s, "hello"); }
4
by: H?seyin | last post by:
I'm calling a function from a C DLL in .NET(C#). The func. seems so: void Sample(char *s) { s= calloc(6,1); strcpy(s, "hello"); }
71
by: desktop | last post by:
I have read in Bjarne Stroustrup that using malloc and free should be avoided in C++ because they deal with uninitialized memory and one should instead use new and delete. But why is that a...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...

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.