User Rating: 0 / 5

Star InactiveStar InactiveStar InactiveStar InactiveStar Inactive
 

Background

This article aims to give an instruction on how to use Visual Studio to debug a C/C++ dynamic library that is called by a PowerBuilder 2019 application.

Here we will show you:

  • How to create a C/C++ dynamic library.
  • How to call this DLL in PowerBuilder application.
  • How to debug the DLL called by the PowerBuilder application.

Description

Let’s take Visual Studio 2017 as an example to create a C/C++ dynamic library NewDLL.dll, which contains two functions:

  • Add: Accepts two int parameters.
  • ComputerName: Accepts a string parameter.

In addition, we will create a simple application with PowerBuilder 2019 to call this NewDLL.dll.

How to create a simple C/C++ dynamic library

1. Start Visual Studio 2017. Select File -> New -> Project to create a NewDLL project

    Select Dynamic-Link Library (DLL) from the listed items.

2. Open the pre-compiled header file pch.h.

    Add export interface code as shown below:

extern "C"
{
	int __stdcall Add(int, int);
	BOOLEAN __stdcall ComputerName(LPTSTR);
}

3. Modify the source file pch.cpp corresponding to the pre-complied header file.

    Add export interface implementation code as shown below:

int __stdcall Add(int x, int y)
{
	return x + y;
}

BOOLEAN __stdcall ComputerName(LPTSTR name)
{
	DWORD dwMaxComputerLength = MAX_COMPUTERNAME_LENGTH * 2;
	return  GetComputerName(name, &dwMaxComputerLength);
}

4. Add a def file to declare the export interface function.

    Right-click the current NewDLL project and select Add -> New Item from the pop-up menu.

    Select Module-Definition File (.def) from the listed items.

5. Modify this def file and add the export interface functions.

Create a PowerBuilder application to call this C/C++ dynamic library

1. Create a new PB application. Add a window and declare local external functions in it as shown below:

FUNCTION int Add (int ix, int iy ) LIBRARY "NewDLL.dll" alias for "Add"

FUNCTION boolean ComputerName (ref string name ) LIBRARY "NewDLL.dll" alias for "ComputerName"

2. Add an Add button and a ComputerName button on the current window to call the functions from the DLL.

integer 	li_rtn   

li_rtn = Add(8, 9 )

Messagebox("Add(8, 9 )", string(li_rtn))

string ls_name
Boolean	lbn_return

ls_name= space(30)  //Initializes the memory space
lbn_return = ComputerName(ls_name)

messagebox("ComputerName", "Return:"+string(lbn_return)+ ", "+ ls_name)

At this moment, you can deploy the application into an exe file or directly run the application in PowerBuilder IDE to check the execution of the current functions.

Use Visual Studio to debug the DLL called by the PowerBuilder application

In most circumstances, when we compile a DLL for PowerBuilder applications, the most troubling part is to debug the functions from the DLL.

So going forward, we will show you how to use the debug function.

Note: In most cases, users cannot use Visual Studio to debug the DLL called by the PowerBuilder application because the corresponding pdb file is not copied to the same PowerBuilder application directory after compiling the DLL.

For example, when debugging the NewDLL.dll called by PowerBuilder application, the corresponding NewDLL.pdb file should be copied to the same PowerBuilder application directory. Please also note that such NewDLL.pdb file is not necessary when deployed to the end users.