Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8203807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T07:41:19+00:00 2026-06-07T07:41:19+00:00

I’m currently exploring DLL exported functions and P/invoke in C#. I’ve created very simple

  • 0

I’m currently exploring DLL exported functions and P/invoke in C#.
I’ve created very simple .dll:

Test.h

#ifndef TEST_DLL_H
#define TEST_DLL_H
extern "C" __declspec(dllexport) const char * __cdecl hello ();
extern "C" __declspec(dllexport) const char * __cdecl test ();    
#endif  // TEST_DLL_H

Test.cpp

#include <stdlib.h>
#include "test.h"
#include <string.h>

const char* hello()
{
    char *novi = (char *)malloc(51);
    strcpy(novi, "Test.");

    return novi;
}

const char * test()
{
    return "Test.";
}

I’ve compiled it and used in C# project like this:

    [DllImport("test.dll", CallingConvention = CallingConvention.Cdecl)]
    public static extern IntPtr hello();

    [DllImport("test.dll", CallingConvention = CallingConvention.Cdecl)]
    public static extern string test();

    private void button1_Click(object sender, EventArgs e)
    {
        MessageBox.Show(test());
        IntPtr a =  hello();
        MessageBox.Show(Marshal.PtrToStringAnsi(a));
    }

But it isn’t working. test() gets called successfully and I get back correct string. But hello() just hangs up the program. If I remove malloc line from the hello() definition and return constant, everything works, so I guess there’s a problem with malloc that I’m now aware of.

Also, somewhere I’ve seen that string shouldn’t be used when return type is char*. If that’s true, why should we use IntPtr?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T07:41:21+00:00Added an answer on June 7, 2026 at 7:41 am

    Functions that return strings across a DLL boundary are very difficult to call reliably from C or C++, it doesn’t get any better when you do it from C#. At issue is how the caller is going to release the string buffer. That needs to be done for hello() but not for test(). Something that’s very hard to guess at. The hello() function requires using the free() function, using the exact same allocator that was used to call malloc(). That can only work when the DLL and the caller share the same CRT implementation. The odds for which are slim.

    The pinvoke marshaller also releases the string buffer, it has to. And does so with the only reasonable choice, CoTaskMemFree(). Which uses the default allocator used by COM. This does not come to a good end, your C code didn’t use CoTaskMemAlloc(). The possible outcome of this depends on the operating system. On Vista and up, your program will die with an AccessViolation, those Windows versions use a strict heap allocator designed to crash misbehaving programs. On XP, you’ll get something between a memory leak and heap corruption, sounds like you hit the second option.

    Declaring the return value as IntPtr will come to a good end. Well, your program won’t crash, you still have a memory leak that you cannot plug. There is no way to call free() reliably. Or use CoTaskMemAlloc() in your C code so that the pinvoke marshaller’s release call will work.

    But realistically, just don’t write C code like this. Always use memory allocated by the caller so there is never a guess who owns the memory. Which requires a function signature similar to this:

    extern "C" __declspec(dllexport) 
    int hello(char* buffer, int bufferSize);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm making a simple page using Google Maps API 3. My first. One marker
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.