none
Urgent help for unloaded library RRS feed

  • Question

  • I use a C# interface program  VS 2010, from it I need to run a cuda extern fn (implemented in VS C++ 2008 as dll library)

    I used the dllimport then in C# I used

     IntPtr hdl = LoadLibrary("cudalib.dll");
        if (hdl != IntPtr.Zero) { some call statments}

    every run the hdl value = 0 ,

    which means unloaded library please how to solve this problem

    • Moved by Leo Liu - MSFT Tuesday, June 7, 2011 6:48 AM Moved for better support. (From:Visual C# Language)
    Monday, June 6, 2011 4:09 AM

Answers

  • using System.Runtime.InteropServices;
    using System;
    
    class call_dll {
    
     [StructLayout(LayoutKind.Sequential, Pack=1)]
     private struct STRUCT_DLL {
      public Int32 count_int;
      public IntPtr ints;
     }
    
     [DllImport("mingw_dll.dll")]
     private static extern int func_dll(
       int an_int,
       [MarshalAs(UnmanagedType.LPArray)] byte[] string_filled_in_dll,
       ref STRUCT_DLL s
      );
     
     public static void Main() {
    
      byte[] string_filled_in_dll = new byte[21];
    
    
      STRUCT_DLL struct_dll = new STRUCT_DLL();
      struct_dll.count_int = 5;
      int[] ia = new int[5];
      ia[0] = 2; ia[1] = 3; ia[2] = 5; ia[3] = 8; ia[4] = 13;
    
      GCHandle gch  = GCHandle.Alloc(ia);
      struct_dll.ints = Marshal.UnsafeAddrOfPinnedArrayElement(ia, 0);
    
      int ret=func_dll(5,string_filled_in_dll, ref struct_dll);
      
      Console.WriteLine("Return Value: " + ret);
      Console.WriteLine("String filled in DLL: " + System.Text.Encoding.ASCII.GetString(string_filled_in_dll));
    
     }
    }
    • Marked as answer by Paul Zhou Monday, June 13, 2011 6:10 AM
    Monday, June 6, 2011 11:19 AM
  •  

    Hi,

     

    Welcome to the MSDN forum.

     

    As far as I know, LoadLibrary is "old-school" now though it is full-documented.

    LoadLibrary Function

    http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/87a3ac29-d8bf-4ce8-98ab-59debc711f9b/

     

    Now, we can import unmanaged dll via DllImport class. Parmjeet has provided a good sample for you.

     

    Sincerely,


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Paul Zhou Monday, June 13, 2011 6:10 AM
    Wednesday, June 8, 2011 9:33 AM

All replies

  • LoadLibrary, is it a custom function ? Can you give full path dll path and try ?
    Malayalam SMS Website http://aspspider.info/smsmallu
    Monday, June 6, 2011 4:59 AM
  • using System.Runtime.InteropServices;
    using System;
    
    class call_dll {
    
     [StructLayout(LayoutKind.Sequential, Pack=1)]
     private struct STRUCT_DLL {
      public Int32 count_int;
      public IntPtr ints;
     }
    
     [DllImport("mingw_dll.dll")]
     private static extern int func_dll(
       int an_int,
       [MarshalAs(UnmanagedType.LPArray)] byte[] string_filled_in_dll,
       ref STRUCT_DLL s
      );
     
     public static void Main() {
    
      byte[] string_filled_in_dll = new byte[21];
    
    
      STRUCT_DLL struct_dll = new STRUCT_DLL();
      struct_dll.count_int = 5;
      int[] ia = new int[5];
      ia[0] = 2; ia[1] = 3; ia[2] = 5; ia[3] = 8; ia[4] = 13;
    
      GCHandle gch  = GCHandle.Alloc(ia);
      struct_dll.ints = Marshal.UnsafeAddrOfPinnedArrayElement(ia, 0);
    
      int ret=func_dll(5,string_filled_in_dll, ref struct_dll);
      
      Console.WriteLine("Return Value: " + ret);
      Console.WriteLine("String filled in DLL: " + System.Text.Encoding.ASCII.GetString(string_filled_in_dll));
    
     }
    }
    • Marked as answer by Paul Zhou Monday, June 13, 2011 6:10 AM
    Monday, June 6, 2011 11:19 AM
  •  

    Hi,

     

    Welcome to the MSDN forum.

     

    As far as I know, LoadLibrary is "old-school" now though it is full-documented.

    LoadLibrary Function

    http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/87a3ac29-d8bf-4ce8-98ab-59debc711f9b/

     

    Now, we can import unmanaged dll via DllImport class. Parmjeet has provided a good sample for you.

     

    Sincerely,


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Paul Zhou Monday, June 13, 2011 6:10 AM
    Wednesday, June 8, 2011 9:33 AM