I built a dll in C (using Code::Blocks) that is imported in a .NET Program (using Visual Studio 2010).
A function (within the dll) returns a string to the main program that shows that string in a Texbox, but the only thing I see is a group of confused characters or nothing at all.

I tried to fix it, but I only found out that the problem is how the string has been built, e.g.
if I write directly something like

```
return "This is a string.";

```

or

```
char* temp = "This is a string.";
return temp;

```

it works perfectly, but if I use something like

```
char temp[30];
strcpy(temp, "This is ");
strcat(temp, "a string.");
return temp;

```

or

```
char temp[30];
temp[0] = 'T';
temp[1] = 'e';
temp[2] = 's';
temp[3] = 't';
temp[4] = '\0';
return temp;

```

or

```
char* temp;
temp = (char*)malloc(sizeof(char)*10);
strcpy(temp, "Test");
return temp;

```

in the first 2 cases, the program shows confused characters, while in the last one it crashes.

---------- [UPDATE] ----------

You are right, that's a stupid error! :O That's the first time I'm trying to use a C DLL onto a .NET Library so I can put the code here (that's only a test, so the code il really short)

That's how I declared the function

```
Declare Function Serial Lib "Test.dll" Alias "_Z9getSerialv" () As String

```

And that's where I call it

```
TextBox1.Text = Serial()

```

And That's the code of the DLL

```
#include "main.h"

char* DLL_EXPORT getSerial()
{
    int n1 = 987, n2 = 123, n3 = 563;
    char num1[30], num2[30], num3[30];
    char *final1 = (char*)calloc(90, sizeof(char));   

    itoa(n1,num1,10);
    itoa(n2,num2,10);
    itoa(n3,num3,10);

    strcpy(final1, num1);
    strcat(final1, "/");
    strcat(final1, num2);
    strcat(final1, "/");
    strcat(final1, num3); 

    return final1;
}

```

I tried to debug the DLL in Code::Blocks, attaching the program I built in VS2010 to it (going in Code::Blocks to Project -> Set Program's Argument -> Host Application), but it doesn't stop at the breakpoint. Trying to debug the main program, VS2010 pop up a MsgBox that says: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

Debugging the DLL in Codeblocks, the app shows the expected result... but if I run the app normally, it crashes (using the same dll, same app, in the same directory). I think that has something to do with code::blocks that (probably) shares the memory between dll and the host app, or more probably something about deallocated memory, but I still don't know how to fix it.