GetProcAddress() private method

private GetProcAddress ( IntPtr hModule, string procedureName ) : IntPtr
hModule System.IntPtr
procedureName string
return System.IntPtr
示例#1
0
        //extern “C” long WINAPI PassThruIoctl
        //(
        //unsigned long ChannelID,
        //unsigned long IoctlID,
        //void *pInput,
        //void *pOutput
        //)

        public bool LoadJ2534Library(string path)
        {
            m_pDll = NativeMethods.LoadLibrary(path);

            if (m_pDll == IntPtr.Zero)
            {
                return(false);
            }

            IntPtr pAddressOfFunctionToCall = NativeMethods.GetProcAddress(m_pDll, "PassThruOpen");

            if (pAddressOfFunctionToCall != IntPtr.Zero)
            {
                Open = (PassThruOpen)Marshal.GetDelegateForFunctionPointer(
                    pAddressOfFunctionToCall,
                    typeof(PassThruOpen));
            }

            pAddressOfFunctionToCall = NativeMethods.GetProcAddress(m_pDll, "PassThruClose");
            if (pAddressOfFunctionToCall != IntPtr.Zero)
            {
                Close = (PassThruClose)Marshal.GetDelegateForFunctionPointer(
                    pAddressOfFunctionToCall,
                    typeof(PassThruClose));
            }

            pAddressOfFunctionToCall = NativeMethods.GetProcAddress(m_pDll, "PassThruConnect");
            if (pAddressOfFunctionToCall != IntPtr.Zero)
            {
                Connect = (PassThruConnect)Marshal.GetDelegateForFunctionPointer(
                    pAddressOfFunctionToCall,
                    typeof(PassThruConnect));
            }

            pAddressOfFunctionToCall = NativeMethods.GetProcAddress(m_pDll, "PassThruDisconnect");
            if (pAddressOfFunctionToCall != IntPtr.Zero)
            {
                Disconnect = (PassThruDisconnect)Marshal.GetDelegateForFunctionPointer(
                    pAddressOfFunctionToCall,
                    typeof(PassThruDisconnect));
            }

            pAddressOfFunctionToCall = NativeMethods.GetProcAddress(m_pDll, "PassThruReadMsgs");
            if (pAddressOfFunctionToCall != IntPtr.Zero)
            {
                ReadMsgs = (PassThruReadMsgs)Marshal.GetDelegateForFunctionPointer(
                    pAddressOfFunctionToCall,
                    typeof(PassThruReadMsgs));
            }

            pAddressOfFunctionToCall = NativeMethods.GetProcAddress(m_pDll, "PassThruWriteMsgs");
            if (pAddressOfFunctionToCall != IntPtr.Zero)
            {
                WriteMsgs = (PassThruWriteMsgs)Marshal.GetDelegateForFunctionPointer(
                    pAddressOfFunctionToCall,
                    typeof(PassThruWriteMsgs));
            }

            pAddressOfFunctionToCall = NativeMethods.GetProcAddress(m_pDll, "PassThruStartPeriodicMsg");
            if (pAddressOfFunctionToCall != IntPtr.Zero)
            {
                StartPeriodicMsg = (PassThruStartPeriodicMsg)Marshal.GetDelegateForFunctionPointer(
                    pAddressOfFunctionToCall,
                    typeof(PassThruStartPeriodicMsg));
            }

            pAddressOfFunctionToCall = NativeMethods.GetProcAddress(m_pDll, "PassThruStopPeriodicMsg");
            if (pAddressOfFunctionToCall != IntPtr.Zero)
            {
                StopPeriodicMsg = (PassThruStopPeriodicMsg)Marshal.GetDelegateForFunctionPointer(
                    pAddressOfFunctionToCall,
                    typeof(PassThruStopPeriodicMsg));
            }

            pAddressOfFunctionToCall = NativeMethods.GetProcAddress(m_pDll, "PassThruStartMsgFilter");
            if (pAddressOfFunctionToCall != IntPtr.Zero)
            {
                StartMsgFilter = (PassThruStartMsgFilter)Marshal.GetDelegateForFunctionPointer(
                    pAddressOfFunctionToCall,
                    typeof(PassThruStartMsgFilter));
            }

#warning This address may not be correct
            pAddressOfFunctionToCall = NativeMethods.GetProcAddress(m_pDll, "PassThruStartMsgFilter");
            if (pAddressOfFunctionToCall != IntPtr.Zero)
            {
                StartPassBlockMsgFilter = (PassThruStartPassBlockMsgFilter)Marshal.GetDelegateForFunctionPointer(
                    pAddressOfFunctionToCall,
                    typeof(PassThruStartPassBlockMsgFilter));
            }

            pAddressOfFunctionToCall = NativeMethods.GetProcAddress(m_pDll, "PassThruStopMsgFilter");
            if (pAddressOfFunctionToCall != IntPtr.Zero)
            {
                StopMsgFilter = (PassThruStopMsgFilter)Marshal.GetDelegateForFunctionPointer(
                    pAddressOfFunctionToCall,
                    typeof(PassThruStopMsgFilter));
            }

            pAddressOfFunctionToCall = NativeMethods.GetProcAddress(m_pDll, "PassThruSetProgrammingVoltage");
            if (pAddressOfFunctionToCall != IntPtr.Zero)
            {
                SetProgrammingVoltage = (PassThruSetProgrammingVoltage)Marshal.GetDelegateForFunctionPointer(
                    pAddressOfFunctionToCall,
                    typeof(PassThruSetProgrammingVoltage));
            }

            pAddressOfFunctionToCall = NativeMethods.GetProcAddress(m_pDll, "PassThruReadVersion");
            if (pAddressOfFunctionToCall != IntPtr.Zero)
            {
                ReadVersion = (PassThruReadVersion)Marshal.GetDelegateForFunctionPointer(
                    pAddressOfFunctionToCall,
                    typeof(PassThruReadVersion));
            }

            pAddressOfFunctionToCall = NativeMethods.GetProcAddress(m_pDll, "PassThruGetLastError");
            if (pAddressOfFunctionToCall != IntPtr.Zero)
            {
                GetLastError = (PassThruGetLastError)Marshal.GetDelegateForFunctionPointer(
                    pAddressOfFunctionToCall,
                    typeof(PassThruGetLastError));
            }

            pAddressOfFunctionToCall = NativeMethods.GetProcAddress(m_pDll, "PassThruIoctl");
            if (pAddressOfFunctionToCall != IntPtr.Zero)
            {
                Ioctl = (PassThruIoctl)Marshal.GetDelegateForFunctionPointer(
                    pAddressOfFunctionToCall,
                    typeof(PassThruIoctl));
            }

            return(true);
        }
        public bool LoadJ2534Library(string path)
        {
            //this DLL does not play nice and causes a loaderlock
            //TODO fix this properly
            string p = Path.GetFileName(path);

            if (p.Contains("BVTX4J32.dll"))
            {
                return(false);
            }
            m_pDll = NativeMethods.LoadLibrary(path);

            if (m_pDll == IntPtr.Zero)
            {
                return(false);
            }

            IntPtr pAddressOfFunctionToCall = NativeMethods.GetProcAddress(m_pDll, "PassThruOpen");

            if (pAddressOfFunctionToCall != IntPtr.Zero)
            {
                Open = (PassThruOpen)Marshal.GetDelegateForFunctionPointer(
                    pAddressOfFunctionToCall,
                    typeof(PassThruOpen));
            }
            else
            {
                return(false);
            }

            pAddressOfFunctionToCall = NativeMethods.GetProcAddress(m_pDll, "PassThruClose");
            if (pAddressOfFunctionToCall != IntPtr.Zero)
            {
                Close = (PassThruClose)Marshal.GetDelegateForFunctionPointer(
                    pAddressOfFunctionToCall,
                    typeof(PassThruClose));
            }
            else
            {
                return(false);
            }

            pAddressOfFunctionToCall = NativeMethods.GetProcAddress(m_pDll, "PassThruConnect");
            if (pAddressOfFunctionToCall != IntPtr.Zero)
            {
                Connect = (PassThruConnect)Marshal.GetDelegateForFunctionPointer(
                    pAddressOfFunctionToCall,
                    typeof(PassThruConnect));
            }
            else
            {
                return(false);
            }

            pAddressOfFunctionToCall = NativeMethods.GetProcAddress(m_pDll, "PassThruDisconnect");
            if (pAddressOfFunctionToCall != IntPtr.Zero)
            {
                Disconnect = (PassThruDisconnect)Marshal.GetDelegateForFunctionPointer(
                    pAddressOfFunctionToCall,
                    typeof(PassThruDisconnect));
            }
            else
            {
                return(false);
            }

            pAddressOfFunctionToCall = NativeMethods.GetProcAddress(m_pDll, "PassThruReadMsgs");
            if (pAddressOfFunctionToCall != IntPtr.Zero)
            {
                ReadMsgs = (PassThruReadMsgs)Marshal.GetDelegateForFunctionPointer(
                    pAddressOfFunctionToCall,
                    typeof(PassThruReadMsgs));
            }
            else
            {
                return(false);
            }

            pAddressOfFunctionToCall = NativeMethods.GetProcAddress(m_pDll, "PassThruWriteMsgs");
            if (pAddressOfFunctionToCall != IntPtr.Zero)
            {
                WriteMsgs = (PassThruWriteMsgs)Marshal.GetDelegateForFunctionPointer(
                    pAddressOfFunctionToCall,
                    typeof(PassThruWriteMsgs));
            }
            else
            {
                return(false);
            }

            pAddressOfFunctionToCall = NativeMethods.GetProcAddress(m_pDll, "PassThruStartPeriodicMsg");
            if (pAddressOfFunctionToCall != IntPtr.Zero)
            {
                StartPeriodicMsg = (PassThruStartPeriodicMsg)Marshal.GetDelegateForFunctionPointer(
                    pAddressOfFunctionToCall,
                    typeof(PassThruStartPeriodicMsg));
            }
            else
            {
                return(false);
            }

            pAddressOfFunctionToCall = NativeMethods.GetProcAddress(m_pDll, "PassThruStopPeriodicMsg");
            if (pAddressOfFunctionToCall != IntPtr.Zero)
            {
                StopPeriodicMsg = (PassThruStopPeriodicMsg)Marshal.GetDelegateForFunctionPointer(
                    pAddressOfFunctionToCall,
                    typeof(PassThruStopPeriodicMsg));
            }
            else
            {
                return(false);
            }

            pAddressOfFunctionToCall = NativeMethods.GetProcAddress(m_pDll, "PassThruStartMsgFilter");
            if (pAddressOfFunctionToCall != IntPtr.Zero)
            {
                StartMsgFilter = (PassThruStartMsgFilter)Marshal.GetDelegateForFunctionPointer(
                    pAddressOfFunctionToCall,
                    typeof(PassThruStartMsgFilter));
            }

            #warning This address may not be correct
            pAddressOfFunctionToCall = NativeMethods.GetProcAddress(m_pDll, "PassThruStartMsgFilter");
            if (pAddressOfFunctionToCall != IntPtr.Zero)
            {
                StartPassBlockMsgFilter = (PassThruStartPassBlockMsgFilter)Marshal.GetDelegateForFunctionPointer(
                    pAddressOfFunctionToCall,
                    typeof(PassThruStartPassBlockMsgFilter));
            }
            else
            {
                return(false);
            }

            pAddressOfFunctionToCall = NativeMethods.GetProcAddress(m_pDll, "PassThruStopMsgFilter");
            if (pAddressOfFunctionToCall != IntPtr.Zero)
            {
                StopMsgFilter = (PassThruStopMsgFilter)Marshal.GetDelegateForFunctionPointer(
                    pAddressOfFunctionToCall,
                    typeof(PassThruStopMsgFilter));
            }
            else
            {
                return(false);
            }

            pAddressOfFunctionToCall = NativeMethods.GetProcAddress(m_pDll, "PassThruSetProgrammingVoltage");
            if (pAddressOfFunctionToCall != IntPtr.Zero)
            {
                SetProgrammingVoltage = (PassThruSetProgrammingVoltage)Marshal.GetDelegateForFunctionPointer(
                    pAddressOfFunctionToCall,
                    typeof(PassThruSetProgrammingVoltage));
            }
            else
            {
                return(false);
            }

            pAddressOfFunctionToCall = NativeMethods.GetProcAddress(m_pDll, "PassThruReadVersion");
            if (pAddressOfFunctionToCall != IntPtr.Zero)
            {
                ReadVersion = (PassThruReadVersion)Marshal.GetDelegateForFunctionPointer(
                    pAddressOfFunctionToCall,
                    typeof(PassThruReadVersion));
            }
            else
            {
                return(false);
            }

            pAddressOfFunctionToCall = NativeMethods.GetProcAddress(m_pDll, "PassThruGetLastError");
            if (pAddressOfFunctionToCall != IntPtr.Zero)
            {
                GetLastError = (PassThruGetLastError)Marshal.GetDelegateForFunctionPointer(
                    pAddressOfFunctionToCall,
                    typeof(PassThruGetLastError));
            }
            else
            {
                return(false);
            }

            pAddressOfFunctionToCall = NativeMethods.GetProcAddress(m_pDll, "PassThruIoctl");
            if (pAddressOfFunctionToCall != IntPtr.Zero)
            {
                Ioctl = (PassThruIoctl)Marshal.GetDelegateForFunctionPointer(
                    pAddressOfFunctionToCall,
                    typeof(PassThruIoctl));
            }
            else
            {
                return(false);
            }

            return(true);
        }