Passing Strings Between Delphi DLL and VBA

Passing Strings Between Delphi DLL and VBA

Unlike other routines, this one doesn’t need to pass the length of the returned string for later trimming in VBA. This is because the Delphi function also tricks VBA into “thinking” the returned string is Length(tmpStr) long when, in reality, it is really still 100-characters long. The trick has to be done though, so that all the nulls after the “0” (zero) character are ignored.

Here’s the Delphi DLL:

Library Example;uses SysUtils;procedure GetStr(A: PChar; B: PChar);stdcall;var StrData:Longword absolute B;  //make StrData share B's memory so it                                  //also holds the address of the first                                  //character in the stringStrDataSizePtr:^Longword;    //pointer to 4-byte memory size--later,                             //we will make it point to the BSTR                              //string size locationtmpStr:string;begin   tmpStr:=A;   tmpStr:=tmpStr + '1234567890';       //make changes to tmpStr   StrPLCopy(B,tmpStr, Length(tmpStr)); //Copy tmpStr to B   StrDataSizePtr:=Ptr(StrData-4);      //point string size location                                        //which starts 4 bytes before                                        //the first char in the string    StrDataSizePtr^:=Length(tmpStr);    //set size to tmpStr length.                                        //this tricks VBA into reading                                        //only length(tmpStr) chars. end;exportsGetStr name 'GetStr';beginend

To use the DLL in VBA (assuming the DLL is in C:Program FilesCommon Files), the declaration would be:

Public Declare Sub GetStr Lib "C:Program FilesCommon FilesExample.dll" (ByVal A As String, ByVal B As String)Public Sub Test_GetStr()Dim A As StringDim B As StringA="This is a test..."B = String(100, vbNullChar) 'reserve lots of space for BGetStr A, BMsgBox "-" & B & "-"End Sub
Share the Post:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as