Nitro for VB
Article by: Torsten Damberg
...or what to do if VB reaches its limits
Introduction:
VB是一種膠強大的語言,VB支持現(xiàn)代的所有技術 is a very powerful language, that supports nearly all modern technologies. What makes VB so powerful? I think, that there are three major reasons for that:
vb通過COM組件可擴展.其他語言中編寫COM組件,在VB中調用Using components written in other languages lets you use the vigorousnesses of these languages with VB.
對VB最大的批判是他自身處理緩慢與其他語言相比(C++, Delphi, Smalltalk等),但是為什么不用VB作為應用程序的框架,關鍵過程在其他語言中寫.The main criticism to VB is, that the native VB language commands are processed slower than in other languages (e.g. C++, Delphi, Smalltalk etc.) But, hey, why not use VB as framework for your application and write time critical functions in another language? This could make your VB application rock. Take my GameAI-Lib as a living example for such a task.
向你展示在VC++中寫COM組件并應用于VB程序In this article, I will show you, how you can create a COM-component using Microsoft Visual C++ 6.0 and how you can use it within your VB application. The only things you need are the compilers (VB and VC) and a little C++ knowledge. You will see, it will be very easy.
Getting started:現(xiàn)在開始
First, we will create a project in VC++:首先,創(chuàng)造一個VC++工程
選擇File->New在Projects選項卡中選 ATL COM AppWizardFrom,工程名為"MyLib"這個名稱也將會是 COM-DLL的名稱。 the VC++ IDE menu choose File->New and activate the tabpage "Projects". From the procets-list choose "ATL COM AppWizard". Give our baby a name. I named our lib "MyLib". This will be the name of the COM-DLL and will also be the name of the TypeLib as you will find it in the VB references list.
按"OK" 后 VC 向導會創(chuàng)建ATL COM工程,ATL 的意思是活動模板庫。After that, press "OK" and VC AppWizard will create a ATL COM Project for us. ATL means Active Template Library and consists of a set of C++ template classes that make implementing COM objects and interfaces much easier.
下一步選擇“動態(tài)連接庫”意味著它將與VB應用程序運行在同一線程中。我們的組件依托于"atl.dll"中,它要方在windows/system目錄。如果在其他機器上運行,需要再注冊。In the next step of the AppWizard choose "Dynamic Link Library(DLL)" as servertype. This will generate a in-process COM-component for us, that means, it will run in the same process environment as our VB application. Our component will have dependencies to the "atl.dll" which is stored in your windows/system folder. You need to redistribute and register it if you want to install your app on another machine. You could also enable "MFC"-support, but if you do this your component will have dependencies to the mfc dlls too. We won‘t do this.
Now press "Finish" and after that "OK". Your project and the source files will be created.
如下:Our project workspace will now look like this:
Forget about that functions, I will not explain them and you will not need them.
我們的類庫沒有任何對象,要添加一個。要更多對象方法也一樣。Let it breath: Now we have a library without any objects in it. Let‘s add our first object (in this tutorial, we will only use one object, but added more objects works analogically to this).
MyLib類上右鍵,選擇"New ATL Object..."。MyLibMove your mouse over the MyLib classes root node and press the right mouse-button. From the context menu choose "New ATL Object...".
在"objects"對象類別中用"Simple Object"作為對象。In the next dialog choose the "objects" category and mark "Simple Object" as object. Press next.
輸入對象名MyObject,類名CMyObject,它將在VB中看到。Enter a short name for your object (this is VC++ internal, you won‘t see this in your VB app). I choosed "MyObject". VC++ will create a C++ class names "CMyObject" from this COM-object. You could rename it, but we will keep that name. You can specify the name of the CoClass at the "CoClass" textbox. This will be the name of the COM-object as it appears in the interface description. That means, this will be the name of the object as you will see it in VB. Press "OK".
可以看到結構"CMyObject()"和交換見面"IMyObject",我們要向其中加入內容。Here is our object. As you see, it only has a constructor "CMyObject()" and a interface "IMyObject". We will now add a function to our object. This function should implement a loop, that adds x times a value to 0 and then return the result. So this is a multiply realised by adding a value. Later on we will use this function to measure the time that our C++ function takes and compare it with VB.
過程將得到兩個參數(shù)The function should get 2 parameters.
lValue as Long ‘ this is the value that is always added
lCount as Long ‘ lCount times will the loop be executed 循環(huán)次數(shù)
rc as Long ‘ this should be the return value 返回值
右鍵單擊節(jié)點選擇“添加方法”Move your mouse over the interface node and right-click it. From the popup-menu choose the option "Add Method...".
方法名我用"Calculate",它會在VB中看見,參數(shù)如下:Give our method a name. I took "Calculate". This will also the name as you will see it in the VB IDE. Type in our parameter list:
long lvalue, long lcount, [out, retval] long* rc
O.K., I think the first 2 parameters are clear if you understand a littlebit about C++. But why is there a third input parameter with such a funny declaration? And where is our return parameter?
你要通過指針獲得結果。COM interface functions always have the same format: They have a variable list of parameters and also return a return-code as HRESULT. If the HRESULT isn‘t S_OK the function failed and a COM error is raised. The VC++ IDL (Interface Decription Language) compiler interpretes the parameters [out, retval] and declares the parameter as an out-parameter and as the (one and only) return value. You must pass the variable as a pointer, because this is the only way to get data out of this function. That‘s the whole magic.
雙擊它,添加如下代碼This is what our function looks like. Double-click it and the VC++ IDE brings you directly to the source of that function. Enter the following code:
STDMETHODIMP CMyObject::Calculate( long lvalue, long lcount, long *rc)
{
long lresult = 0;
for ( long i = 0; i < lcount; i++)
{
lresult = lresult + lvalue;
}
*rc = lresult;
return S_OK;
}
編譯Compiling: We are finished. The last thing we have to do is compiling the component.
編譯選"Win 32 - Release MinSize"(菜單:編譯->編譯 MyLib.dll),VC++會編譯編譯組件并注冊Choose the compile option "Win 32 - Release MinSize" and compile your program (menu: Build->Build MyLib.dll) The VC++ compiler will build your component and register it, so it is ready to use.
在VB中:創(chuàng)建一工程,添加一按扭,選擇工程->引用 選你的組件Using it with VB: Create a new VB project and add a button to it. From the menu, choose project->references and pick our component:
在按扭中加入代碼:Press OK and you are ready to use the library in our sample VB project. Within the button-code add the following code:
Dim oMyObject As New MyObject
MsgBox Format(oMyObject.Calculate(2, 5000000))
未完成,你可以添加更多的Now we are ready with this tutorial. Try the Sample (check the link at the button). In the sample, I have implemented the same functionality in VC++ and in VB. Compare the speed of both version. But try it more than once, because our VC is loaded for the first time into memory. This will cause our first execution a littlebit slower. Remember, that this calculation is pretty uncomplex. You can get a lot more out of it.
I hope this tutorial will help you develope VC++ COM-objects for VB. If you have any question about using other data-types than "long", ask me or take a look at msdn online or the VC++ online manual (OLE types).
在VC++中調試組件:在VB中創(chuàng)建project1.exe,在VC++設置編譯選項"Win 32 - Release MinSize" 變?yōu)?"Win 32 - Debug"。Tip - Debugging our component with VC++: How can I debug this object if it is used from VB? This is easy: Make an executeable (project1.exe) from your VB project. In VC++ set the compile option from "Win 32 - Release MinSize" to "Win 32 - Debug".
運行程序,在代碼中設置短點,當程序運行,在VC++中可看到停在短點處Specify the VB project.exe as executeable for the debug session and set a breakpoint in your code. Now press "F5" to "run" your component. After that the VB application is started and if you reach the breakpoint the VC++ debugger will stop at the C++ code. That‘s it.
Download the source: You can download the source of the tutorial
here.