ResourceString (C++)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example shows how to load Delphi resource strings from a C++ application.

To build and test this example:

  1. Create a Delphi package where you can define a resource string:
    1. Create a new Delphi package, and save it as "DelphiPackage".
    2. Add a new Delphi unit to your package, and save this unit as "DelphiUnit".
  2. Create a C++ console application to load and show your Delphi resource string:
    1. On the Project Manager, right-click your project group and select Add New Project.
    2. On the New Items dialog box, select C++Builder Projects on the tree view and, on the list of C++Builder projects, select Console Application and click OK.
    3. On the New Console Application dialog box that opens, click OK to use the default C++ console application settings.
    4. Save your main C++ file as "main.cpp", your precompiled header file as "CppApplicationPCH.h", and your project as "CppApplication".
    5. Select Project > Options > C++ Linker and enable the "Link with the Delphi Runtime Library" option.

Code

Add a "Message" resource string to the DelphiUnit.pas file:

unit DelphiUnit;

interface

resourcestring
  Message = 'This is a Delphi resource string.';

implementation

end.

Then right-click your DelphiPackage project on the Project Manager and select Build.

Add the generated Delphi package library file to your C++ console application:

  1. Right-click your CppApplication project on the Project Manager and select Add.
  2. On the dialog box that opens, select the DelphiPackage.lib file that you previously built and click OK. You may find this file inside "C:\Users\Public\Documents\Embarcadero\Studio\19.0\Dcp".
    Note: If you cannot see the expected file, ensure that the file filter in the dialog box is "Any file (*.*)".

In your main.cpp file:

  1. Include the standard iostream header file.
  2. Include DelphiUnit.hpp (generated from your DelphiUnit.pas file when you built your Delphi package).
    Note: You must include iostream before you include DelphiUnit.hpp.
  3. In the main function, use std::wcout << Delphiunit_Message << std::endl; to print your Delphi resource string to the standard output.
  4. Add std::cin.get(); afterwards so that your console application does not exit execution until you press a key.
#pragma hdrstop
#pragma argsused

#ifdef _WIN32
#include <tchar.h>
#else
  typedef char _TCHAR;
  #define _tmain main
#endif

#include <iostream>
#include <DelphiUnit.hpp>

 int _tmain(int argc, _TCHAR* argv[]) 
{
        std::wcout << Delphiunit_Message << std::endl;
        std::cin.get();
        return 0;
}

Now you can run your C++ console application. A console should open and display your Delphi resource string:

This is a Delphi resource string.

Uses

See Also