none
Late binding C DLL with c# RRS feed

  • Question

  • Hello fellas,

    I am developing an application where I need to take user inputs, process them and give the results...here for processing I'm using C DLL which user wants me to use. i.e. I don't know the DLL which I'm going to use at compile time. That C Dll may contain different methods such as Add, subtract, multiply,etc...But I don't know their prototypes as I said earlier I'm using DLL configured by the user with some OpenDialog().

    So is there way to achieve this? I have searched a lot on net but got the reply as if its a COM or .NET DLL it can be possible using reflection emit. It means for C DLL it can not be....:(

    Can you please help me as I am very new to this stuff...

    Thanks in advance...
    Thursday, March 26, 2009 10:40 AM

Answers

  • santoshdarekar said:

    I know that it is not possible to call a C function if I dont know its signature.. That is my problem, I want some method or a way to get this signature from the DLL (C DLL). Is it possible to do so??

    Generally, no. C DLLs don't contain any metadata you can query. So you can either invent your own metadata format to describe the functions exported from the DLLs, or consider a more modern technology to base your extensibility on. Both COM and .NET provide rich metadata that makes this so much easier.


    Mattias, C# MVP
    • Marked as answer by Zhi-Xin Ye Wednesday, April 1, 2009 7:03 AM
    Thursday, March 26, 2009 12:43 PM
    Moderator
  • To late bind and execute native functions given the library path, function name and signature use LoadLibrary/GetProcAddress/ Marshall.GetDelegateFromFunctionPointer. If you really have to do this follow Mattias's advice and invent some format (XML based for example) to express the function return values and parameters, however be aware that supporting anything more then simple built in types (int,short, char*, etc) will quickly become a mess.

    HTH


    • Marked as answer by Zhi-Xin Ye Wednesday, April 1, 2009 7:03 AM
    Friday, March 27, 2009 4:31 PM

All replies

  • You cannot call a 'C' function if you don't know what arguments it takes.  Dynamically loading a DLL and calling a function is already supported in C# with the [DllImport] attribute.
    Hans Passant.
    Thursday, March 26, 2009 11:24 AM
    Moderator
  • Thank you for the prompt reply.

    I know that it is not possible to call a C function if I dont know its signature.. That is my problem, I want some method or a way to get this signature from the DLL (C DLL). Is it possible to do so??

    Thanks in advance :)
    Thursday, March 26, 2009 11:36 AM
  • I think that the design for your application is bad. Users should not allowed select libraries as they wish, the number of operations that can be performed is fixed for an application.

    You can analyze all the requirements and build an application that can suffice all users. Give them options to select the feature they like.

    Also note that this is not the correct forum.
    Thursday, March 26, 2009 12:05 PM
  • Actually the thing is that, I don't want to restrict user of my application to do the predefined things. He/she should be able to do any operations defined in their libraries through my application. The ultimate aim of my application is to provide interface to the user where he can pass his/her data to the functions defined in the libraries(C DLLs) and call them at runtime.

    The only thing I wanted to know is can it be done and if yes then how?
    Thursday, March 26, 2009 12:10 PM
  • This will not be possible, you can provide dummy functions for your library(dll) which is already known to your application. These functions as such will not do anything but the user can program these functions to do something and use your application to call their library.
    Thursday, March 26, 2009 12:34 PM
  • santoshdarekar said:

    I know that it is not possible to call a C function if I dont know its signature.. That is my problem, I want some method or a way to get this signature from the DLL (C DLL). Is it possible to do so??

    Generally, no. C DLLs don't contain any metadata you can query. So you can either invent your own metadata format to describe the functions exported from the DLLs, or consider a more modern technology to base your extensibility on. Both COM and .NET provide rich metadata that makes this so much easier.


    Mattias, C# MVP
    • Marked as answer by Zhi-Xin Ye Wednesday, April 1, 2009 7:03 AM
    Thursday, March 26, 2009 12:43 PM
    Moderator
  • Dear mattias,

    Thanks for your kind reply....

    I will find some other way....Meanwhile, if you get any new ideas, please let me know...

    Friday, March 27, 2009 4:26 AM
  • To late bind and execute native functions given the library path, function name and signature use LoadLibrary/GetProcAddress/ Marshall.GetDelegateFromFunctionPointer. If you really have to do this follow Mattias's advice and invent some format (XML based for example) to express the function return values and parameters, however be aware that supporting anything more then simple built in types (int,short, char*, etc) will quickly become a mess.

    HTH


    • Marked as answer by Zhi-Xin Ye Wednesday, April 1, 2009 7:03 AM
    Friday, March 27, 2009 4:31 PM
  • Just want to add serveral examples for the method mentioned by p.b.a.

    Use LoadLibrary/GetProcAddress/ Marshall.GetDelegateFromFunctionPointer in C#

    Type-safe Managed wrappers for kernel32!GetProcAddress
    http://blogs.msdn.com/jmstall/archive/2007/01/06/Typesafe-GetProcAddress.aspx

    But there are a few small problems in its client-side code. I made some modification, and put a working example in All-In-One Code Framework. Please refer to the CSLoadLibrary example.

    Regards,
    Jialiang Ge
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Saturday, April 4, 2009 2:33 AM
    Moderator