日期:2014-05-20  浏览次数:20931 次

郁闷死,没错呀.....关于调用C++写的.DLL
我是从VB中改过来的,原VB写法:
////////bas
Attribute   VB_Name   =   "Module1 "
Option   Explicit

Declare   Function   LoadLibrary   Lib   "kernel32 "   Alias   "LoadLibraryA "   (ByVal   lpLibFileName   As   String)   As   Long
Declare   Function   GetProcAddress   Lib   "kernel32 "   (ByVal   hModule   As   Long,   ByVal   lpProcName   As   String)   As   Long
Declare   Function   SetWindowsHookEx   Lib   "user32 "   Alias   "SetWindowsHookExA "   (ByVal   idHook   As   Long,   ByVal   lpfn   As   Long,   ByVal   hmod   As   Long,   ByVal   dwThreadId   As   Long)   As   Long

Public   Const   WH_CBT   =   5
////////frm
Option   Explicit

Private   Sub   Form_Load()

    Dim   hHookDLL   As   Long
    Dim   pHookFunction   As   Long
    Dim   hSystemHook   As   Long
   
    App.TaskVisible   =   False
   
    hHookDLL   =   LoadLibrary( "StickyApp32.DLL ")
   
    If   hHookDLL   =   0   Then
        MsgBox   "Could   not   locate   StickyApp32.DLL ",   vbOKOnly   Or   vbCritical,   "StickyApp32 "
        End
    End   If
   
    pHookFunction   =   GetProcAddress(hHookDLL,   "HookFunction ")
    hSystemHook   =   SetWindowsHookEx(WH_CBT,   pHookFunction,   hHookDLL,   0)
   
End   Sub

Private   Sub   Form_Unload(Cancel   As   Integer)
    Cancel   =   True
End   Sub


//////////////////////////////////CSHARP
=====================我写的:
using   System;
using   System.Collections.Generic;
using   System.ComponentModel;
using   System.Data;
using   System.Drawing;
using   System.Text;
using   System.Windows.Forms;
using   System.Runtime.InteropServices;//这是用到DllImport时候要引入的包

namespace   WindowsApplication4
{
   
        public   partial   class   Form1   :   Form
        {
                const   int   WH_CBT   =   5;
[DllImport( "kernel32 ",   EntryPoint   =   "GetProcAddress ",   SetLastError   =   true)]
                public   static   extern   IntPtr   GetProcAddress(IntPtr   hModule,   string   lpProcName);
                [DllImport( "kernel32 ",   EntryPoint   =   "LoadLibrary ",   SetLastError   =   true)]
                public   static   extern   IntPtr   LoadLibrary(string   lpLibName);
                [DllImport( "User32.dll ",   CharSet   =   CharSet.Auto)]
                public   static   extern   IntPtr   SetWindowsHookEx(int   type,   IntPtr   hook,   IntPtr   instance,   int   threadID);