Accessing a method from a DLL from a C # program

I have a C program and I created a dll file. I am using Windows Vista and Visual C ++.

Now I need to access the method from this DLL, from the Main () method of C # code. What are the steps for doing this?

So far I have added the dll file as a link, after which what should I do?

This is just an example:

int main1( void ) {
  prinf("Hello World");
}

Please note that this class also makes us out of other .lib functions, but I was able to successfully create a DLL from it. (I do not know how relevant this is)

Now I need to access this method from my C # Main ();

[STAThread]
static void Main()
{
  // I need to call that main1() method here

  Application.EnableVisualStyles();
  Application.SetCompatibleTextRenderingDefault(false);
  Application.Run(new Form1());
}
+5
source share
3 answers

. , dll ++ #, . , Hans Passant , DLL #.

DLL-, # , DLL, DLL #,

  • "" > " ".
  • DLL, .
  • .
  • DLL, #, .
  • , .

DLL bin\Debug # , - , Microsoft Installer.

, , DLL, (. C C ++ Language Executables DLL __declspec (dllexport)). extern "C" ( , .cpp, , , extern "C"):

extern "C"
{
    __declspec (dllexport) void __cdecl Foo(const char* arg1);
}

...

void Foo(const char* arg1)
{
    printf ("Hello %s !", arg1);
}

__declspec (dllexport) , / - DLL. __cdecl , ( "" ).

# DLL:

class Program
{
    [DllImport("mydll.dll")]
    internal static extern void Foo(string arg1);

    static void Main()
    {
        Program.Foo ("Pierre");
    }
}

Platform Invoke Tutorial, gory.

+5

Platform Invoke [DllImport].

0

P/Invoke aka pinvoke aka "Invoke Platform":

http://msdn.microsoft.com/en-us/library/aa288468(v=vs.71).aspx

0

All Articles