OberonPlace.com Forums  

Go Back   OberonPlace.com Forums > Developer Forums > VBA > CorelDRAW/Corel DESIGNER VBA

Thread Tools Search this Thread Display Modes
Old 11-06-2005, 14:44
Posts: n/a
Default How I can call my function in DLL from VBA code?

Does anybody know how I can call my function in DLL from VBA code?
Reply With Quote
Old 14-06-2005, 11:59
Alex's Avatar
Alex Alex is offline
Join Date: Nov 2002
Posts: 1,940
Blog Entries: 4

If you are talking about a Windows API function, then it's easy to do. You just need to find a Visual Basic declaration of the function on the net and put it in the macro. For example, here is how you would read a value from an .ini file. Let's assume you have a file called c:\settings.ini which has the following content:

Here is how you would extract this data from the ini file using Windows API GetPrivateProfileString function:

Option Explicit

Private Declare Function GetPrivateProfileString Lib "kernel32" _
        Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, _
        ByVal lpKeyName As String, ByVal lpDefault As String, _
        ByVal lpReturnedString As String, ByVal nSize As Long, _
        ByVal lpFileName As String) As Long

Private Function ReadSetting(ByVal strSection As String, ByVal strKey As String) As String
    Dim strVal As String
    Dim nLen As Long
    strVal = Space$(200)
    nLen = GetPrivateProfileString(strSection, strKey, "", strVal, Len(strVal), "C:\Settings.ini")
    ReadSetting = Left$(strVal, nLen)
End Function

Sub Test()
    MsgBox ReadSetting("Person", "Name")
    MsgBox ReadSetting("Person", "Gender")
End Sub
You can get standard Windows API definitions for Visual Basic from many internet sites. Here is one: http://www.mentalis.org/apilist/apilist.php

If you want to call functions from your own DLL, you can do it the same way. Just make sure you define the parameters correctly (add ByVal keyword to parameters which are passed in by value and not by pointer/reference).
Reply With Quote
Old 14-06-2005, 15:27
Posts: n/a

Thanks. I'll try this one.
Reply With Quote
Old 15-06-2005, 07:11
ddonnahoe's Avatar
ddonnahoe ddonnahoe is offline
Senior Member
Join Date: Jan 2004
Location: Louisville, KY
Posts: 552
Send a message via ICQ to ddonnahoe Send a message via AIM to ddonnahoe Send a message via MSN to ddonnahoe Send a message via Yahoo to ddonnahoe

That's pretty cool. but how would you do it in reverse? For instance, How would you write to the settings.ini file. I have a macro that you have to enter a customer name, address, phone number, and contact.

What I'd like to see happen is any new customer that is added in the form would be added to the .ini file and during form_initialize(), I would like to recall all of thos items stored in the .ini file and add them to my combobox.

I assume that the .ini file would look something like this...
Name=ABC Company
Address1=123 Street
Address2=Somewhere, USA 12345
I'm trying to figure out how there would be multiple entries or would this be better done from a .txt file?
Waiting for a ride in the T.A.R.D.I.S.
Reply With Quote
Old 15-06-2005, 17:59
Posts: n/a

Hello, Alex.

I tried your code changed for my own function int Run(int) in my own DLL written in MSVC2K3 but had error:

Run-time error '453': Can't find DLL entry point Run in MyDLL.dll

The test application written in MSVC2K3 called my function perfectly.

In VB I declared my function like this:

Private Declare Function Run Lib "C:\MOE\MyDLL\Debug\MyDLL" (ByVal l As Long) As Long

And called like this:

Run (0)

What mistake I did? Warning: I don't know anything about VB...

And small question. How I can call function by the address returned by WINAPI GetProcAddress?

Reply With Quote
Old 23-06-2005, 14:40
Posts: n/a

Hello! Thanks to all but I already solved problem myself. Probably a thing interesting to community. As I expected C++ compiler renamed my function in DLL by adding a prefix and suffix. So function void Run(void) really has name ?Run@@YGHXZ. When I changed declaration in VB my function has called perfectly.
Reply With Quote
Old 23-06-2005, 15:02
Alex's Avatar
Alex Alex is offline
Join Date: Nov 2002
Posts: 1,940
Blog Entries: 4

To avoid C++ name mangling, you need to use this:

extern "C" void Run(void)
Also add the function "Run" in the .def file in your project. This way the name of exported function will be exactly "Run" and not something else. If you don't add the export entry in the .def file and use just __declspec(dllexport) like in:

extern "C" __declspec(dllexport) void Run(void){}
then your function will be exported as "Run@0" where "0" is the size of data on stack for parameters. Since your function has no parameters it will be suffixed with "@0". If you declare it as, say, "void Run(int v)" then it will be exported as "Run@4" since int takes 4 bytes.

I hope this helps.
Reply With Quote

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Recreating curves in VBA code Alex CorelDRAW/Corel DESIGNER VBA 19 05-03-2011 20:37
HOW-TO call coreldraw12 find dialog from VBA code???? wOxxOm CorelDRAW/Corel DESIGNER VBA 4 02-03-2008 08:32
Text ENCODE Craig Tucker CorelDRAW/Corel DESIGNER VBA 10 26-01-2005 13:59
Printing Raw Prn Files Mark Code Critique 2 16-09-2004 16:15
How can I extract a piece of a bitmap object using VBA code oswaldon Corel Photo-Paint VBA 2 25-04-2004 19:37

All times are GMT -5. The time now is 13:40.

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2021, Jelsoft Enterprises Ltd.
Copyright © 2011, Oberonplace.com