dll添加引用进来!里面的里面就可以调用乐呀

解决方案 »

  1.   

    你把dll加到引用中
    就可了
      

  2.   

    好象不可以吧,可以给DLL传参数
      

  3.   

    here is an example:1. TestVoid.cpp (run vcvars32.bat first, then cl /LD TestVoid.cpp):
    #include <iostream.h>extern "C"  
    {
        __declspec(dllexport) void Test(void* v, int* n) {
           char* c = (char*)v;
    *n = 10;
    for(int i=0; i < *n; i++)
       c[i] = 'A'+i;
        }
    }2. TestVoid.cs:
    using System;
    using System.Runtime.InteropServices;class Class1
    {
            [STAThread]
            static void Main(string[] args)
            {     
                byte[] b = new byte[100];
        int n;
        Test(b,out n);
        Console.WriteLine(n);
        for (int i=0;i < n; i++)
    Console.WriteLine((char)b[i]);
            }        [DllImport("TestVoid.dll")]
            public static extern void Test(byte[] bs, out int size);
    }
    3. output:
    E:\labs\csharp>TestVoid
    10
    A
    B
    C
    D
    E
    F
    G
    H
    I
    J
    4. if your dll has specical calling convention, then you need to use additional parameters for DllImport:[DllImport("msvcrt.dll", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.Cdecl)]see
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemRuntimeInteropServicesDllImportAttributeClassTopic.asphttp://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore/html/vcwlkSysimportAttributeTutorial.asp
      

  4.   

    sorry, you cannot access a variable, but you can call methods to retrieve the values
      

  5.   

    I found one way to access a variable in dll, but I can only get the address of the variable, I do not know how I can get the value of it .
    [DllImport("kernel32.dll")]
       private extern static IntPtr LoadLibrary(string fileName) ;[DllImport("kernel32.dll")]
       private extern static IntPtr GetProcAddress(IntPtr handle, string procName) ;protected void GetVariable()
    {
       IntPtr handle = LoadLibrary("Winscard") ;
       IntPtr pci = GetProcAddress(handle, "g_rgSCardT0Pci") ;
       //How can I get the value of variable then, I don't know.
    }
      

  6.   

    you are correct, but you need to export that global variable too , otherwise, GetProcAddress return 0, also you need to use unsafe modeyou can see the exports from a dll with dumpbin.exe from
    C:\Program Files\Microsoft Visual Studio\VC98\Bintry1. TestVoid.cpp:extern "C"  
    {
        __declspec(dllexport) int g_rgSCardT0Pci = 100;
        __declspec(dllexport) void Test(void* v, int* n, int *g) {
           char* c = (char*)v;
    *n = 10;
    for(int i=0; i < *n; i++)
       c[i] = 'A'+i;
    *g = g_rgSCardT0Pci;
        }
    }
    2. TestVoid.cs:
    using System;
    using System.Runtime.InteropServices;class Class1
    {
            [STAThread]
            static void Main(string[] args)
            {
                Console.WriteLine("Starting...");
                
                byte[] b = new byte[100];
        int n;
        int v;
        Test(b,out n, out v);
        Console.WriteLine(n);
        for (int i=0;i < n; i++)
    Console.WriteLine((char)b[i]);     Console.WriteLine(v);
                GetVariable();
            }
    unsafe static protected void GetVariable()
    {
        IntPtr handle = LoadLibrary("TestVoid.dll") ;
        IntPtr pci = GetProcAddress(handle, "g_rgSCardT0Pci") ;
        //Console.WriteLine("{0:X}", pci);
    int* p = (int*)pci;
    Console.WriteLine(*p);
        FreeLibrary(handle);
    }
            [DllImport("TestVoid.dll")]
            public static extern void Test(byte[] bs, out int size, out int v);
    [DllImport("kernel32.dll")]
        private extern static IntPtr LoadLibrary(string fileName) ; [DllImport("kernel32.dll")]
        private extern static IntPtr GetProcAddress(IntPtr handle, string procName) ;
    [DllImport("kernel32.dll")]
        private extern static void FreeLibrary(IntPtr handle) ;
    }