Click here to Skip to main content
15,895,799 members
Home / Discussions / C#
   

C#

 
GeneralRe: Cash for Help! Pin
MicealG6-Sep-07 5:25
MicealG6-Sep-07 5:25 
GeneralRe: Cash for Help! Pin
J4amieC6-Sep-07 12:17
J4amieC6-Sep-07 12:17 
QuestionWeb Service returning DateTime one hour out Pin
funkymint6-Sep-07 1:08
funkymint6-Sep-07 1:08 
QuestionReally simple question on Drop Down List Boxes Pin
Infernojericho6-Sep-07 0:42
Infernojericho6-Sep-07 0:42 
AnswerRe: Really simple question on Drop Down List Boxes Pin
Sandeep Akhare6-Sep-07 0:54
Sandeep Akhare6-Sep-07 0:54 
GeneralRe: Really simple question on Drop Down List Boxes Pin
Infernojericho6-Sep-07 1:03
Infernojericho6-Sep-07 1:03 
GeneralRe: Really simple question on Drop Down List Boxes Pin
Sandeep Akhare6-Sep-07 1:25
Sandeep Akhare6-Sep-07 1:25 
QuestionDynamically create a delegate signature Pin
Leonardo Pelisoli6-Sep-07 0:42
Leonardo Pelisoli6-Sep-07 0:42 
Hello,

I'm new to C#, so please bear with me if the question is obvious and/or stupid.

I have a C# (.NET Framework 2.0) application that needs to call functions in an unmanaged C/C++ DLL. The name of the DLL is unknown at compile time (so I can't use the DllImportAttribute directly). The solution I adopted uses the native LoadLibrary/GetProcAddress combination, as seen in the code snippet below.

using System;
using System.Runtime.InteropServices;

namespace Simple
{
    class DLLPlugin
    {        
        [DllImport("kernel32", EntryPoint = "LoadLibrary")]
        private static extern IntPtr LoadLibrary(string lpLibFileName);
        [DllImport("kernel32", EntryPoint = "GetProcAddress", SetLastError = true)]
        private static extern IntPtr GetProcAddress(IntPtr hModule, string lpProcName);
        [DllImport("kernel32", EntryPoint = "FreeLibrary", SetLastError = true)]
        private static extern bool FreeLibrary(IntPtr hModule);

        public delegate void VoidVoidDelegate();
        
        private VoidVoidDelegate RunDelegate;
        private IntPtr moduleRef;

        public DLLPlugin(string dllName)
        {
            moduleRef = LoadLibrary(dllName);
            IntPtr pfn = GetProcAddress(moduleRef, "Run");
            RunDelegate = (VoidVoidDelegate)
                Marshal.GetDelegateForFunctionPointer(pfn, typeof(VoidVoidDelegate));
        }

        ~DLLPlugin()
        {
            FreeLibrary(moduleRef);
        }

        public void Run()
        {
            RunDelegate();
        }
    }
}


In the code you'll see that I declared a delegate,
public delegate void VoidVoidDelegate();
private VoidVoidDelegate RunDelegate;

and then used it in the constructor to store the function I'm going to call in the DLL. In order to do that, I had to use the delegate type twice: once as a parameter for GetDelegateForFunctionPointer and then again to cast the return value.

Now, this example calls a function named Run that receives no arguments and returns nothing (void), as specified by the delegate signature.

Now, my problem is that I don't know this signature at compile time. Maybe the first time it runs, the Run function will receive two strings and return a bool. Or maybe it will receive a string, two bools, an int and return a double. Or maybe something else.

What I need is to supply the return and argument types, and then create at runtime the right kind of delegate to use. I would then use this delegate created at runtime to supply the correct signature to the GetDelegateForFunctionPointer method and to correctly cast the return value. Not to mention to call my function in the DLL, of course.

If someone has any suggestions, I'd greatly appreciate the help. Code examples as well to illustrate, since I'm not at all familiar with using Reflection.Emit (I'm assuming it's going to go that way?).
AnswerRe: Dynamically create a delegate signature Pin
TJoe6-Sep-07 8:55
TJoe6-Sep-07 8:55 
GeneralRe: Dynamically create a delegate signature Pin
Leonardo Pelisoli6-Sep-07 22:23
Leonardo Pelisoli6-Sep-07 22:23 
QuestionUPnP InvokeAction Pin
MicealG6-Sep-07 0:27
MicealG6-Sep-07 0:27 
Questioncompare MS Word Documents Pin
Nitin.raj6-Sep-07 0:05
Nitin.raj6-Sep-07 0:05 
AnswerRe: compare MS Word Documents Pin
originSH6-Sep-07 0:08
originSH6-Sep-07 0:08 
GeneralRe: compare MS Word Documents Pin
Nitin.raj6-Sep-07 0:11
Nitin.raj6-Sep-07 0:11 
GeneralRe: compare MS Word Documents Pin
originSH6-Sep-07 0:22
originSH6-Sep-07 0:22 
GeneralRe: compare MS Word Documents Pin
mav.northwind6-Sep-07 0:26
mav.northwind6-Sep-07 0:26 
QuestionMethod to set time Pin
iet20005-Sep-07 23:59
iet20005-Sep-07 23:59 
AnswerRe: Method to set time Pin
Vikram A Punathambekar6-Sep-07 0:14
Vikram A Punathambekar6-Sep-07 0:14 
GeneralRe: Method to set time Pin
iet20006-Sep-07 1:19
iet20006-Sep-07 1:19 
QuestionCompare Word docs Pin
Nitin.raj5-Sep-07 23:53
Nitin.raj5-Sep-07 23:53 
AnswerRe: Compare Word docs Pin
originSH6-Sep-07 0:05
originSH6-Sep-07 0:05 
Questioncrystal report export problem Pin
sakthi dasan5-Sep-07 23:38
sakthi dasan5-Sep-07 23:38 
QuestionLicencing Problem urgent Pin
Nishad855-Sep-07 23:34
Nishad855-Sep-07 23:34 
AnswerRe: Licencing Problem urgent Pin
MicealG6-Sep-07 1:27
MicealG6-Sep-07 1:27 
QuestionString.IsNullOrEmpty Pin
nasambur5-Sep-07 23:31
nasambur5-Sep-07 23:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.