I've been stuck on this one for days, I'm hoping that someone in here has been through this hurdle before and remembers how to get past it. Seriously, I IMPLORE you to help me with this. I've tried posts to several newsgroups, countless hours spent searching blogs and documentation - even a paid site like Experts Exchange, no one has been able to answer my question and help me get this working. I'm really getting burned out on this and could seriously use some help. Thank you!!!

That said, here's some background: I'm trying to access some logic in a Win32 C EXE. I've gotten 1 _very_ simple test function to work, but as soon as I try to do anything with pointers nothing will work (I keep getting AccessViolationExceptions).

I've been told that you can't P/Invoke with a C EXE but this test works just fine. Here's the C code for the test function that works:
-------------------- Start 1st Example ----------------------------------------------------------
int __declspec(dllexport) myAdd(int a, int b)
return a + b;

then in .Net I use the following:

EntryPoint = "myAdd",
ExactSpelling = false,
CallingConvention = CallingConvention.Cdecl)]
static extern int c_add(int a, int b);

I can call "c_add(5, 2)" and get 7 no problem.

-------------------- End 1st Example ----------------------------------------------------------

My next test was to do some text manipulation.

-------------------- Start 2nd Example ----------------------------------------------------------
void __declspec(dllexport) myText(char* output)
sprintf(output, "Hello world!");

The .net code:

EntryPoint = "myText",
ExactSpelling = false,
CallingConvention = CallingConvention.Cdecl)]
static extern void c_text(StringBuilder output);

StringBuilder sb = new StringBuilder(15);//plenty of room
myText(sb); <-- throws an AccessViolationException

-------------------- End 2nd Example ----------------------------------------------------------

How am I supposed to do text manipulation through P/Invoke I've tried sending byte[], I've tried using the Marshal class to allocate the memory first, I've tried using an IntPtr, I've tried changing the CallingConvertion, tried the CharSet=CharSet.Auto (and all the other CharSets), tried sending my parameters as "ref", "out" and just normal...I don't know what else to try.

If it makes a difference, here are the commands I'm using to compile the C code:
Compile: "cl -Zi -Od -c -W2 -DWIN32 myCcode.c"
Link: "link -debug -out:myCcode.exe myCcode.obj"

<plea voiceOf="Princess Lea">

Help me MSDN Community, you're my only hope.


Thank you so much for your help,


Re: Common Language Runtime P/Invoke a C EXE throws an AccessViolationException. _Please_ Help


You shouldn't expose or access functions in an EXE as that is non-standard. If you choose to do so then you will have to live with the fact that it might or might not work. If it does, then great. If it doesn't, then fix it so it works in a standard way.

I tried your example using a regular DLL and it works just fine. I did notice that you didn't extern "C" your function so if this were, say, a C++ app then it wouldn't work probably. Then again you should have seen the same problem with your int call. I also notice that you reference myText in your managed code but that shouldn't compile as you called it something else when you imported it. Here is the code that I have working:


#define TESTDLL_API __declspec(dllexport)

extern "C" TESTDLL_API void myText(char* buffer)


sprintf(buffer, "Hello World!");



[DllImport("TestDLL", EntryPoint="myText")]

private static extern void c_myText(StringBuilder buffer);

static void Main(string[] args)


StringBuilder sb = new StringBuilder(15);


string strResults = sb.ToString();


Michael Taylor - 10/24/07