Click here to Skip to main content
15,910,277 members
Home / Discussions / C#
   

C#

 
QuestionWindows Form (C#.net) Pin
onecomputerguy4-Aug-10 0:36
onecomputerguy4-Aug-10 0:36 
AnswerRe: Windows Form (C#.net) Pin
OriginalGriff4-Aug-10 1:04
mveOriginalGriff4-Aug-10 1:04 
QuestionHello Sir i am muthu from kumbakonam Pin
Muthumca3-Aug-10 21:24
Muthumca3-Aug-10 21:24 
GeneralRe: Hello Sir i am muthu from kumbakonam Pin
OriginalGriff3-Aug-10 21:56
mveOriginalGriff3-Aug-10 21:56 
QuestionCall a function from a dynamically loaded C dll [Answered] Pin
Bernhard Hiller3-Aug-10 19:47
Bernhard Hiller3-Aug-10 19:47 
AnswerRe: Call a function from a dynamically loaded C dll Pin
Gonzalo Cao3-Aug-10 20:40
Gonzalo Cao3-Aug-10 20:40 
GeneralRe: Call a function from a dynamically loaded C dll Pin
Bernhard Hiller3-Aug-10 21:02
Bernhard Hiller3-Aug-10 21:02 
AnswerRe: Call a function from a dynamically loaded C dll PinPopular
Alan N3-Aug-10 23:37
Alan N3-Aug-10 23:37 
Bernhard,
Dynamically loading an unmanaged dll uses an almost direct translation of C code.

In the example note the use of
1) the UnmanagedFunctionPointer attribute on the SomeFunction delegate declaration
2) the Marshal.GetDelegateForFunctionPointer method to convert the raw function pointer to the C# friendly delegate.

C#
using System;
using System.Runtime.InteropServices;
internal class DynamicLoad {
  [DllImport("kernel32.dll", SetLastError = true)]
  private static extern IntPtr LoadLibrary(String dllToLoad);

  [DllImport("kernel32", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
  private static extern IntPtr GetProcAddress(IntPtr hModule, string procName);

  [DllImport("kernel32.dll", SetLastError = true)]
  private static extern bool FreeLibrary(IntPtr hModule);
  
  // Assuming CharSet.Ansi, you may be Unicode.
  [UnmanagedFunctionPointer(CallingConvention.StdCall, SetLastError = false, CharSet = CharSet.Ansi)]
  internal delegate int SomeFunction(string Param1);

  private IntPtr dllPtr;
  internal SomeFunction someFunction;

  internal Boolean Load(String dllPath) {
    dllPtr = LoadLibrary(dllPath);
    // Int32 code = Marshal.GetLastWin32Error();
    // TODO: error handling
    if (dllPtr != IntPtr.Zero) {
      try {
        someFunction = (SomeFunction)Marshal.GetDelegateForFunctionPointer(
          GetProcAddress(dllPtr, "SomeFunction"),
          typeof(SomeFunction));
      } catch (ArgumentNullException) {
        // SomeFunction was not found
        Unload();
      }
    }
    return (dllPtr != IntPtr.Zero);
  }

  internal void Unload() {
    bool result = FreeLibrary(dllPtr);
    // TODO: error handling
    dllPtr = IntPtr.Zero;
  }
}

Alan.
GeneralRe: Call a function from a dynamically loaded C dll Pin
Bernhard Hiller4-Aug-10 2:11
Bernhard Hiller4-Aug-10 2:11 
GeneralRe: Call a function from a dynamically loaded C dll Pin
Phil J Pearson4-Aug-10 3:24
Phil J Pearson4-Aug-10 3:24 
QuestionRe: Call a function from a dynamically loaded C dll Pin
Gonzalo Cao4-Aug-10 20:28
Gonzalo Cao4-Aug-10 20:28 
AnswerRe: Call a function from a dynamically loaded C dll Pin
Bernhard Hiller4-Aug-10 21:19
Bernhard Hiller4-Aug-10 21:19 
GeneralRe: Call a function from a dynamically loaded C dll Pin
Gonzalo Cao4-Aug-10 22:12
Gonzalo Cao4-Aug-10 22:12 
QuestionHow to change a theam in c# ( winform ) Pin
Krishna Varadharajan3-Aug-10 19:34
Krishna Varadharajan3-Aug-10 19:34 
GeneralRe: How to change a theam in c# ( winform ) Pin
Łukasz Nowakowski3-Aug-10 22:38
Łukasz Nowakowski3-Aug-10 22:38 
GeneralRe: How to change a theam in c# ( winform ) Pin
Smithers-Jones4-Aug-10 1:19
Smithers-Jones4-Aug-10 1:19 
GeneralRe: How to change a theam in c# ( winform ) Pin
Łukasz Nowakowski4-Aug-10 1:28
Łukasz Nowakowski4-Aug-10 1:28 
QuestionPls help! how to prevent user to delete prefix value in my texbox Pin
crisjala3-Aug-10 19:29
crisjala3-Aug-10 19:29 
AnswerRe: Pls help! how to prevent user to delete prefix value in my texbox Pin
Gerry Schmitz3-Aug-10 19:46
mveGerry Schmitz3-Aug-10 19:46 
AnswerRe: Pls help! how to prevent user to delete prefix value in my texbox Pin
OriginalGriff3-Aug-10 22:03
mveOriginalGriff3-Aug-10 22:03 
QuestionCheck whether value is there in a column table Pin
seeism3-Aug-10 19:06
seeism3-Aug-10 19:06 
AnswerRe: Check whether value is there in a column table Pin
seeism3-Aug-10 21:03
seeism3-Aug-10 21:03 
AnswerRe: Check whether value is there in a column table Pin
David Skelly3-Aug-10 22:21
David Skelly3-Aug-10 22:21 
QuestionProper naming style for global variable? Pin
Necrowizard3-Aug-10 13:09
Necrowizard3-Aug-10 13:09 
AnswerRe: Proper naming style for global variable? Pin
I Believe In GOD3-Aug-10 13:18
I Believe In GOD3-Aug-10 13:18 

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.