Calling external function in C

Discussion in 'C Programming' started by Elstree, Mar 7, 2008.

  1. Elstree

    Elstree Guest

    Hi everybody,

    Is it possible to
    1. call a function from a dll made with .NET (C#)
    2. from a program written in plain (as in: not .NET) C or C++?

    To be more specific, this is what I have.

    1.
    C# file Class.cs:

    using System; //and some other packages

    namespace myns
    {
    public class Class
    {
    public Class()
    {
    }

    public static byte[] getByteArray(
    string name,
    int index,
    string format,
    ref int n
    ) {
    //Code returning some byte array.
    }
    }
    }

    Compiling it, I get myns.dll.

    2.
    C file mycaller.c:

    #include<windows.h>
    #include<stdio.h>
    #include"mycaller.h"

    typedef byte* (*FunctionPointer)(char*, int, char*, int*);

    #ifdef __cplusplus
    extern "C" {
    #endif

    byte* myfunc() {

    //load the library
    HINSTANCE m_libraryHandle = LoadLibrary("myns.dll");
    if(m_libraryHandle != NULL) {
    printf("Library loaded\n");
    }
    else {
    printf("Library not loaded\n");
    return NULL;
    }

    byte* result = NULL;

    //get the function pointer
    FunctionPointer functionptr =
    (FunctionPointer)GetProcAddress(m_libraryHandle,
    "getByteArray");

    if(functionptr != NULL) {
    printf("Function pointer is OK\n");

    int n = -1;
    //call the function from the loaded library
    result = (*functionptr)("fubar1", 0, "fubar2", &n);

    //some code modifying the result
    }
    else {
    printf("Function pointer is NULL\n");
    }

    //free the library
    FreeLibrary(m_libraryHandle);
    printf("Library freed\n");

    return result;
    };

    #ifdef __cplusplus
    }
    #endif

    Now, the problem. Library gets loaded but the function pointer is
    allways NULL.

    1. Should the method call getByteArray() in Class.cs be "externalized"
    somehow
    in order for the call to be successful? If so, how do I "externalize" it?

    2. Is the name of the function in GetProcAddress() wrong? Should I put the
    namespace (myns) and class name (Class) in front of the method name? If
    so, how (I mean what delimiters do I use)?

    3. Something else?

    All help will be greatly appreciated. Thanks.
    Elstree, Mar 7, 2008
    #1
    1. Advertising

  2. Elstree

    Ian Collins Guest

    Elstree wrote:
    > Hi everybody,
    >
    > Is it possible to
    > 1. call a function from a dll made with .NET (C#)
    > 2. from a program written in plain (as in: not .NET) C or C++?
    >

    The answer lies on a .net group rather than here.

    --
    Ian Collins.
    Ian Collins, Mar 7, 2008
    #2
    1. Advertising

  3. Elstree said:

    > Hi everybody,
    >
    > Is it possible to
    > 1. call a function from a dll made with .NET (C#)


    Try asking Q1 in microsoft.public.dotnet.languages.csharp

    > 2. from a program written in plain (as in: not .NET) C or C++?


    Yes, this is certainly possible. The way I do it is to write the DLL code
    in standard C, construct a .def file that describes all the symbols I want
    to export (that's function names, basically), and tell the implementation
    to compile it as a DLL. That should produce an import library as well as
    the DLL itself, and I simply link the import library into the program that
    calls the DLL, just as if it were a normal static library. This is all
    very easy, and doesn't require Win32-pollution of the source base.

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
    Richard Heathfield, Mar 7, 2008
    #3
  4. "Elstree" wrote:

    > Is it possible to
    > 1. call a function from a dll made with .NET (C#)
    > 2. from a program written in plain (as in: not .NET) C or C++?


    Depends on your OS, compiler, etc. Not a C-language matter
    and hence not on-topic in this group.

    Read the manuals, and also ask in these groups:

    microsoft.public.dotnet.languages.csharp
    microsoft.public.dotnet.languages.vc
    microsoft.public.win32.programmer
    comp.os.ms-windows.programmer.win32
    comp.os.ms-windows.programmer.misc

    --
    Cheers,
    Robbie Hatley
    lonewolf aatt well dott com
    www dott well dott com slant user slant lonewolf slant
    Robbie Hatley, Mar 7, 2008
    #4
  5. Elstree

    Richard Guest

    "Robbie Hatley" <> writes:

    > "Elstree" wrote:
    >
    >> Is it possible to
    >> 1. call a function from a dll made with .NET (C#)
    >> 2. from a program written in plain (as in: not .NET) C or C++?

    >
    > Depends on your OS, compiler, etc. Not a C-language matter
    > and hence not on-topic in this group.
    >
    > Read the manuals, and also ask in these groups:
    >
    > microsoft.public.dotnet.languages.csharp
    > microsoft.public.dotnet.languages.vc
    > microsoft.public.win32.programmer
    > comp.os.ms-windows.programmer.win32
    > comp.os.ms-windows.programmer.misc


    Out of curiosity, on average, how many times a day do you post OT replies?
    Richard, Mar 8, 2008
    #5
  6. Elstree

    jacob navia Guest

    Elstree wrote:
    > Hi everybody,
    >
    > Is it possible to
    > 1. call a function from a dll made with .NET (C#)


    Yes, using the COM interface

    > 2. from a program written in plain (as in: not .NET) C or C++?
    >


    Yes, lcc-win does this.


    > To be more specific, this is what I have.
    >


    [snip]

    That can't work. You have to create an object, and use COM
    to talk with it. This can be done automatically, and we have
    developed an interface that does this.

    If you want to buy it please contact
    q-software-solutions.com, or contact me by private mail.

    If not, you can always developed it yourself. It is not that
    complicated if you know COM.


    --
    jacob navia
    jacob at jacob point remcomp point fr
    logiciels/informatique
    http://www.cs.virginia.edu/~lcc-win32
    jacob navia, Mar 8, 2008
    #6
  7. Elstree

    jacob navia Guest

    Richard Heathfield wrote:
    > Elstree said:
    >
    >> Hi everybody,
    >>
    >> Is it possible to
    >> 1. call a function from a dll made with .NET (C#)

    >
    > Try asking Q1 in microsoft.public.dotnet.languages.csharp
    >
    >> 2. from a program written in plain (as in: not .NET) C or C++?

    >
    > Yes, this is certainly possible. The way I do it is to write the DLL code
    > in standard C, construct a .def file that describes all the symbols I want
    > to export (that's function names, basically), and tell the implementation
    > to compile it as a DLL. That should produce an import library as well as
    > the DLL itself, and I simply link the import library into the program that
    > calls the DLL, just as if it were a normal static library. This is all
    > very easy, and doesn't require Win32-pollution of the source base.
    >


    You misunderstood the question. He wants to link with a dll built
    with .NET.

    He doesn't want to build a dll or link with a dll using C

    --
    jacob navia
    jacob at jacob point remcomp point fr
    logiciels/informatique
    http://www.cs.virginia.edu/~lcc-win32
    jacob navia, Mar 8, 2008
    #7
  8. jacob navia said:

    <snip>

    > You misunderstood the question. He wants to link with a dll built
    > with .NET.
    >
    > He doesn't want to build a dll or link with a dll using C


    I must admit I never thought I'd say this, but - yes, you're right.

    I've made a note on the calendar.

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
    Richard Heathfield, Mar 8, 2008
    #8
  9. Richard Heathfield wrote:
    > jacob navia said:
    >
    > <snip>
    >
    >> You misunderstood the question. He wants to link with a dll built
    >> with .NET.
    >>
    >> He doesn't want to build a dll or link with a dll using C

    >
    > I must admit I never thought I'd say this, but - yes, you're right.
    >
    > I've made a note on the calendar.

    Was that remark realy neccessary? A simple "yes, you're right" would have
    done.

    Bye, Jojo
    Joachim Schmitz, Mar 9, 2008
    #9
  10. In article <fr0vkl$dul$>,
    Joachim Schmitz <> wrote:
    >Richard Heathfield wrote:
    >> jacob navia said:
    >>
    >> <snip>
    >>
    >>> You misunderstood the question. He wants to link with a dll built
    >>> with .NET.
    >>>
    >>> He doesn't want to build a dll or link with a dll using C

    >>
    >> I must admit I never thought I'd say this, but - yes, you're right.
    >>
    >> I've made a note on the calendar.

    >Was that remark realy neccessary? A simple "yes, you're right" would have
    >done.


    Yes, a simple "yes, you're right" would have been minimally standards
    compliant, but would not have been in the spirit of this NG. We expect
    better from our warriors.

    Surely, even you know tht...
    Kenny McCormack, Mar 9, 2008
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Raskolnikow
    Replies:
    1
    Views:
    348
    Michiel Salters
    Jul 18, 2003
  2. Scott Allen
    Replies:
    8
    Views:
    10,895
    Scott Allen
    May 2, 2004
  3. Andreas Lagemann
    Replies:
    8
    Views:
    456
    Mike Wahler
    Jan 10, 2005
  4. tiwy
    Replies:
    0
    Views:
    425
  5. John Smith

    inner function calling external function

    John Smith, Mar 17, 2005, in forum: Javascript
    Replies:
    0
    Views:
    83
    John Smith
    Mar 17, 2005
Loading...

Share This Page