C code from C#

ashraf

Member
Joined
Feb 24, 2004
Messages
7
Could someone tell me how to access C code from C#?

a sample would surely help...

I have some C files which compile using a makefile..how can i access the program from C#?
 
heres an example from MSDN:

C#:
using System;
using System.Runtime.InteropServices;
class MyClass 
{
   [DllImport("User32.dll")]
   public static extern int MessageBox(int h, string m, string c, int type);

   public static int Main() 
   {
      string myString; 
      Console.Write("Enter your message: ");
      myString = Console.ReadLine();
      return MessageBox(0, myString, "My Message Box", 0);
   }
}
 
Thanks...I am able to do that...My problem is that the C# code doesnt recognize the entry point in the dll...

To make a dll, I created a win32 project called Try in visual studio
with the following code in the main file...

#include "stdafx.h"
#include <stdio.h>

void func(void)
{
printf("Inside func!");
}

I compile this as a dll and then try to call func() from C#..It is giving the following error...
Unhandled Exception: System.EntryPointNotFoundException: Unable to find an entry point named func in DLL Try.dll.
 
Change
C#:
[DllImport("YourDLL.dll")]
public static extern void func();
to:
C#:
[DllImport("YourDLL.dll", EntryPoint="func",  SetLastError=true,
CharSet=CharSet.Unicode, ExactSpelling=true,
CallingConvention=CallingConvention.StdCall)]
public static extern void func();

if that doesnt work I cant help you any further, my C/C++ knowledge is minimal.
 


Write your reply...
Back
Top