Improve the performance of VB call DLL functions by using the type library

zhaozj2021-02-11  234

This article is initiated in Shuimu Tsinghua BBS (SMTH.org), please keep relevant information, thank you!

Improve the performance of VB call DLL functions by using the type library

Roachcock @ smth mailto: chen3feng@163.com, chen3fengx@163.com, chen3fengx@hotmail.com VB Although it is a very fun thing, it is limited, limited to VB in certain aspects. MS provides two kinds. Two ways to enhance VB's function, one is to call COM components, the other is to call the DLL function to call COM, everyone is familiar, in the Project-> Reference or Project-> Component menu pop-up dialog box Select The type library or ActiveX control can join Project. Call the DLL function is also very simple, you need to use the declare function declaration first: Declare Function MessageBox lib "user32" alias "messageboxa" (_ byval hwnd as long, byval lptext as string Byval LPCAPTION As String, _ Byval WTYPE AS long) AS Long then calls like internal functions: MessageBox me.hwnd, "HEHE", "TEST", 0 below I analyze it in the calling process in the above call, in the end What happened. We know that VB's string is a Unicode string, each character accounts for two bytes, but open Winuser.h we can see the true definition of MessageBoxa, WinuseraPi Int WinApi MessageBoxa (hwnd hwnd, lpcstr lptext , LPCSTR LPCAPTION, UINT UTYPE;

LPCSTR is a const char * type, a pointer to the single-byte ANSI string, compiles the Project of this function into an EXE, file, and see the VC belt to see how to reference the function actually except MSVBVM60. Dll. What happened in this middle? You can know the code generated by disassembly tracking. VB must call __vbastrtoansi to convert the string into an ANSI string, and pass the resulting ANSI string to an interior. Function (generally referred to as DLLFunctionCall), this function is really calling a function in the DLL. If the function is called, you should use the string parameters that pass to the DLL function (such as getWindowText what is what is more complicated, the VB compiler will Call a function called __vbastrtounicode, transform the parameters into unicode, then pass to the function. Maybe you will say that MessageBox does not change the parameters passed to it, do not need to be converted into unicode. Yes, you know, I know Also knowing, but the VB compiler does not know that the Declare statement of the declaration function does not point to the direction of the parameter passed. Maybe you will say, my program is only running under Winnt, Win2k, WinXP, and the Unicode string used inside them. , I call the Unicode version of MessageBoxW is not better? Sorry, since Win9X only fully implements the ANSI function, it is basically no function to implement the Unicode version, and Winnt is all achieved. To take care of Win9X, VB The String in the statement is as an ANSI string. // Need unicode, but relies on the ANSI operating system, poor VB! This, I think you have seen the declare statement in the end that affects efficiency. Ok. Let us Start a new process! [IDL language and type library] Type library should be very familiar with VB programmers, here I only introduce the IDL language. IDL (Interface Definition Language) is mainly used for COM and CORBA The interface definition language is used to describe various interfaces, just a description language rather than a programming language. IDL style is similar to the C.IDL language can not only describe more data types, but also describe the passing direction of the parameters, therefore The above two questions are no longer a problem. Generally, the IDL language is used to describe the COM interface. In fact, IDL can also be used to describe global functions. Let's take an example, he also holds that Messagebo X bar opens VC6, create a new UTITITY Project, name is called myTLB, create a new text file, the name is still called myTLB.IDL, enter the following: [// square bracket indicates an IDL property, used to describe some additional information, To connect to the following things. UUID ("9F7B1BC0-28C6-11D7-8F56-0080C8F0A08C") // generate a new Guid with the VC belt, don't be like this] Library mylib // library description A type library, mylib is the name of the library {[Dllname ("user32.dll") file: // Since the following module describes the function, the name of the DLL library is required] module test // Test is the module's name {[ Entry ("MessageBoxw") // entry property indicates the entry of the following function, which can be the name or serial number of the function] long __stdcall message (hwnd hwnd, [in] lpcwstr msg, [in] lpcwstr title, long flags;

// __stdcall represents the call convention of the function, the back is the parameter table // [in] indicates that the parameter is only incoming, [OUT] [in] indicates that the parameter is only incoming, [in, out] indicates that both incoming, out, class push // For a simple value type, it can be omitted, the default is [in] // There is a function to write}} Press F7 to compile, a new type library is born! // BTW, call the MIDL compiler directly However, I think many people don't like the command line, :) Introduce it into our project, do not need to declare to call MessageBox. It can be found without excessive conversion. And because of the clear point of parameters In the direction of delivery, it is not necessary to assume that the parameters will be changed. The generated code will be more efficient, avoiding the expenditure of DLLFunctionCall, saving time. Other functions can also be processed, Windows 98 Resource Kit takes a Win.TLB, including common Wind OWS type, constant, and definition of the API, it is very convenient to get rid of the use of the DECLARED statement. You can search in the Tianwang (http://e.pku.edu.cn). But this TLB is an ANSI version (Win98 Well, if you need a Unicode version, you have to write the IDL file to compile the DLL function that is not type library. You can find this example you come to you, and the number of functions is full. I wish you a happy speech!

转载请注明原文地址:https://www.9cbs.com/read-3712.html

New Post(0)