calling C function from C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vikrammiddha
    New Member
    • Aug 2007
    • 1

    #1

    calling C function from C#

    hi,
    i have a dll which i made of a C program..
    #include <stdio.h>

    extern "C"
    {

    __declspec(dlle xport) void DisplayHelloFro mDLL(char *input,char *output )
    // int n;
    {
    // char *name = "";
    //*name = *input;
    printf ("Hello from DLL !\n");

    printf ("string is %s", input);
    output = input;

    }

    }


    now i need to call this function from the C# code...


    using System;
    using System.Runtime. InteropServices ;

    class HelloWorld
    {
    [DllImport("test 1.dll")]
    public static extern void DisplayHelloFro mDLL(System.Str ing i,System.String o);

    static void Main ()
    {
    Console.WriteLi ne ("This is C# program");
    System.String i = "teststring ";
    System.String o = "";
    DisplayHelloFro mDLL (i,o);
    Console.WriteLi ne("output is {0}",o);
    }
    }

    Can anyone tell me how can i pass the string parameter to C function where it is accepting it as char* ... there are 2 variables... one is input and other one is output which i need to get the value back in C#.
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    The way you are doing it did not work?
    You may have to say "ref" in your function declaration.

    [DllImport("test 1.dll")]
    public static extern void DisplayHelloFro mDLL(ref String i, ref String o);

    Comment

    Working...