top of page
stosonqouisortosan

Best Practices for Calling dll Function in vb.net



VB, VBA, VBScript, JavaScript, are programming languages and scripting languages to write and automate tasks in Office and Windows operating systems. They are easy to implement and good for rapid development. These have API support for doing all sorts of works provided by Windows and Office. Some APIs that are not available in the built-in libraries or some APIs which are custom needed by a user cannot be fulfilled. These types of situations call for a custom-made library file. C programming language does not have any restrictions to access low-layer system calls and utility functions. Thus user requires to implement the required function in C programming and that function can be called from Visual Besic.


Visual Basic compiler /linker generates the same type of output binary as of C compiler. Both compilers push arguments from right to left and call the function. Low level machine instructions are the same. So applications written in Visual Basic can access C functions. Functions have to reside inside a dynamic link library(dll). Program loader loads dynamic link library either from current path or from absolute location or searchs all locations given in PATH variable.




how to call dll function in vb.net



Apart from dll function address, Visual Basic also need function prototype so that it can push input variables in stack before calling the function. Calling C Functions should be of type stdcall. Stdcall cleans its own stack or callee cleans the stack. So all cleanups are done in Dll side. All Windows DLL works like this. We have below example showing how VB can call a function using call by value and call by reference.


We are creating a Win32 Dynamic Link Library VC++ project. We are selecting this project as Simple Dynamic Link Library Project.VC++ editor will create the necessary project files including header and C/C++ implementation file.DllMain() function will be implemented by default by the editor. We need to add CallCByVal, CallCByRef prototypes and the body of the functions.


We are creating a simple VB application project to call the C function.We have added a button in the main form and added the calling code inside the click event of the button.This is however not limited to VB. VBA, VB script and Javascript can call the function in the same way as VB does.


For Visual Basic applications (or applications in other languages such as Pascal or Fortran) to call functions in a C/C++ DLL, the functions must be exported using the correct calling convention without any name decoration done by the compiler


__stdcall creates the correct calling convention for the function (the called function cleans up the stack and parameters are passed from right to left) but decorates the function name differently. So, when __declspec(dllexport) is used on an exported function in a DLL, the decorated name is exported.


The __stdcall name decoration prefixes the symbol name with an underscore ( _ ) and appends the symbol with an at sign (@) character followed by the number of bytes in the argument list (the required stack space). As a result, the function when declared as:


Because there is no way to override where the stack cleanup occurs, you must use __stdcall. To undecorate names with __stdcall, you must specify them by using aliases in the EXPORTS section of the .def file. This is shown as follows for the following function declaration:


For DLLs to be called by programs written in Visual Basic, the alias technique shown in this topic is needed in the .def file. If the alias is done in the Visual Basic program, use of aliasing in the .def file is not necessary. It can be done in the Visual Basic program by adding an alias clause to the Declare statement.


One difference between the code for DLL2 and the code for DLL1 is that GetCpuSpeed() is declared using __stdcall. This is required for any C/C++ function that you want to use with VB. Here is what MSDN says about the __stdcall calling convention:


The Microsoft C++ compilers encode the names of symbols in C++ programs to include type information in the name. This is called "name decoration", or "name mangling". The purpose of this is to ensure type-safe linking. The C++ language allows function overloading where functions with the same name are only distinguished from one another by the data types of the arguments to the functions. Name decoration enables the linker to distinguish between different versions of overloaded functions because the names of the functions are encoded or decorated differently.


Programs created using a .NET language dynamically load unmanaged DLL's at runtime, so the DLL name and characteristics must be specified in the managed code. When creating a native Fortran DLL that can be called by a .NET application, compile and link with one of the following -ml options: winapi, msvc, lf95, or fc. DLLs built with the -ml msvb option can be called from VB.NET applications. DLLs built with the -ml options lf90, bc, or bd cannot be called from .NET languages.


If you're using the Call .NET DLL methods plug-in to call .NET DLL functions to perform work during an install, one of the things that will be useful is to be able to update the Details list in the installer, from the DLL, while a .NET DLL function call is executing. Below is some VB.NET code, easily converted to C# at DEVELOPERfusion, that provides a method DetailPrint that adds an entry to the end of the Details list.


The VB.NET source code is for a complete namespace and class and includes an example function ChopString to test with. You'll also find an NSIS test script. Lastly, a zip file is attached with the complete Visual Studio 2005 project for the test .NET DLL and the NSIS test script. Also included in the zip is ListViewDeclarations.vb that has all the declarations necessary for working with List-view controls from VB.NET. This file also has the DetailPrint method...all this is contained in a VB.NET #Region you can drop in your .NET DLL.


Hello I'm not a freebasic programmer but I think your structure will be free when the function ends. probably you need to use a global structure. I'm not sure if freebasic provide structure pointer or need to use a function like strptr or varptr like old vb6 or something like @Mystructure. I think is not a good practices to return an structure from a library. the correct way should be that you pass your structure's pointer an be filled/modify inside the library.


Thanks for all the "views" - but no replies! It occurs to me that as it stands above it is not an easy one to get your teeth into. First question: has anyone used XE7 C++ to compile a VCL dll and then successfully called the functions for VB.Net application? (or even using XE7 Delphi VCL ?).


I could e-mail any interested parties a zip file of all the files that make a project that consistently demonstrates the problem. It unzips to C:\ucc (not nice, I know but this is chosen by the customer). There is a VB.Net exe file called SW_724407_VBNet_test. The dll project is configured to use this file to run the dll. When you run the dll you get a small window. You can click on the graph button and a chart is displayed (can do this several times - no problem). Then when you click on the version number it calls the dll, returns with the version number correctly. Next click on the graph button generates an exception. It does this every time.


I have tried another test: I created a dll based on no framework with a single user function GetDllSoftwareVersionVB() (same code as above but not basing it on vcl). I confirmed this works ok with the VB.Net exe file 100% of the time (it does, as before). Then I replaced the return 123456789; line with return GetAnInt() function and I created another new dll based on the VCL framework which contains just this function GetAnInt() which returns a number. (I used 444888). Now when the VB.Net exe is run it calls the GetDllSoftwareVersionVB() in the "no framework" dll which then calls the function GetAnInt() in the VCL based dll. With this scenario, yet again, I get a crash on the VB.Net chart if it is updated after the dll call has been made. Shame - I had hoped this might be a work around to get me out of what is starting to look like a genuine hole....


I have passed this problem to a friend who is much more familiar with VB.Net programming than me. He has found that the function calls to the VCL C++ dll I have written cause the VB.Net floating point package to be "screwed up". If, after you call each VCL C++ dll function call you then do a VB.Net call to the function _fpreset() this "resets" (whatever that actually means) the VB.Net floating point package and all is well. _fpreset() needs to be declared as follows (it is in file: msvcr71.dll)


So there is a fix / work around - but why is this call necessary? What isthe vcl C++ dll doing when function calls are made that causes the floatingpoint package of VB.Net to require this reset? It is good to have a fix butit would be so much better if I understood the fix and therefore had theconfidence that the problem is not going to come back and bite me in someother form, further down the line....


The reason seems to be that the Embarcadero compiler turns off the error mask bits in the floating point control register, ie it enables fp exceptions. This state can persist long after the DLL has been called, and cause errors in any other code (not just the calling application).


  • Please login to add favorites.Dismiss this notice","authentication_redirect":"","dev_mode":"","logged_in":"","user_id":"0","authentication_redirect_url":"https:\/\/www.sqlservercentral.com\/wp-login.php"};/* ]]> *//* */ const sites = ['www.red-gate.com','www.sqlservercentral.com','documentation.red-gate.com','sqlmonitormetrics.red-gate.com'];window.dataLayer = window.dataLayer [];function gtag()dataLayer.push(arguments);gtag('js', new Date());gtag('config', 'UA-90206-6');gtag('config', 'G-QQKLT0M52F');gtag('config', 'UA-90206-169', 'linker': 'domains': sites );/* *//* */.avatar_overlays pbackground:rgba(0,0,0,);color:.wpuap_tooltip:hover .wpuap_tooltip_contentdisplay:inline;position:absolute;color:;border:1px solid;background:.avatar_container [class^=icon-],.avatar_container [class*=" icon-"]color:!important#ci-modal,.ci_controlsbackground-color:!important ArticlesEditorialsStairwaysForumsForums homeActive threadsLatest topicsMost popularLeaderboardScriptsQotDBooksBlogs Register

  • Login

  • Write for us Menu ArticlesEditorialsStairwaysForumsForums homeActive threadsLatest topicsMost popularLeaderboardScriptsQotDBooksBlogs Write for us Register

  • Login

  • Home

  • Forums

  • SQL Server 7,2000

  • General

  • Calling a VB DLL from a Stored Procedure

Post reply Calling a VB DLL from a Stored Procedure Yasir Masood 2ff7e9595c


0 views0 comments

Recent Posts

See All

Baixar 30 juz muzammil

Como Baixar 30 Juz Muzammil Se você está procurando uma maneira de baixar 30 Juz Muzammil, uma bela e reconfortante recitação do Alcorão...

Comments


bottom of page