Welcome, Guest
Guest Settings
Help

Thread: Windows API import in delphi package for C++ with UNICODE



Permlink Replies: 1 - Last Post: Dec 28, 2016 10:00 AM Last Post By: Remy Lebeau (Te... Threads: [ Previous | Next ]
John Clark

Posts: 13
Registered: 5/21/15
Windows API import in delphi package for C++ with UNICODE
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 27, 2016 6:12 AM
I have a package that is written in Delphi that I use in C++ Builder. The hpp generated does not seem to be correct for UNICODE Below are the imports and the corresponding C++ header generation. The UNICODE define uses the char * instead of the wchar_t. How should the imports be declared in the package to get the correct header generation, wchar_t for UNICODE?

//declaration
function    AVIFileOpenA(var ppfile: PAVIFILE; szFile: LPCSTR; uMode: UINT; lpHandler: PCLSID): HResult; stdcall;
function    AVIFileOpenW(var ppfile: PAVIFILE; szFile: LPCWSTR; uMode: UINT; lpHandler: PCLSID): HResult; stdcall;
function    AVIFileOpen(var ppfile: PAVIFILE; szFile: LPCSTR; uMode: UINT; lpHandler: PCLSID): HResult; stdcall;
 
// import
function    AVIFileOpenA(var ppfile: PAVIFILE; szFile: LPCSTR; uMode: UINT; lpHandler: PCLSID): HResult; stdcall; external AVIFILDLL;
function    AVIFileOpenW(var ppfile: PAVIFILE; szFile: LPCWSTR; uMode: UINT; lpHandler: PCLSID): HResult; stdcall; external AVIFILDLL;
function    AVIFileOpen(var ppfile: PAVIFILE; szFile: LPCSTR; uMode: UINT; lpHandler: PCLSID): HResult; stdcall;  overload; external AVIFILDLL name 'AVIFileOpenA';
 
// generated code
extern "C" int __stdcall AVIFileOpenA(PAVIFile &ppfile, char * szFile, unsigned uMode, Mmole2::PGUID lpHandler);
extern "C" int __stdcall AVIFileOpenW(PAVIFile &ppfile, System::WideChar * szFile, unsigned uMode, Mmole2::PGUID lpHandler);
extern "C" int __stdcall AVIFileOpen(PAVIFile &ppfile, char * szFile, unsigned uMode, Mmole2::PGUID lpHandler);
 
#if defined(UNICODE)
#pragma option push -w-inl
inline int __stdcall AVIFileOpen(PAVIFile &ppfile, char * szFile, unsigned uMode, Mmole2::PGUID lpHandler) <----- how to structure delphi imports so UNICODE call uses wchar_t
{
	return AVIFileOpenW(ppfile, szFile, uMode, lpHandler);
}
#pragma option pop
 
#else
#pragma option push -w-inl
inline int __stdcall AVIFileOpen(PAVIFile &ppfile, char * szFile, unsigned uMode, Mmole2::PGUID lpHandler)
{
	return AVIFileOpenA(ppfile, szFile, uMode, lpHandler);
}
#pragma option pop
 
#endif
 
Remy Lebeau (Te...


Posts: 7,723
Registered: 12/23/01
Re: Windows API import in delphi package for C++ with UNICODE
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 28, 2016 10:00 AM   in response to: John Clark in response to: John Clark
John wrote:

I have a package that is written in Delphi that I use in C++ Builder.
The hpp generated does not seem to be correct for UNICODE Below are
the imports and the corresponding C++ header generation. The UNICODE
define uses the char * instead of the wchar_t.

You used LPCSTR (aka 'const char*') instead of LPCTSTR (aka 'const TCHAR*')
in your declaration of AVIFileOpen(). LPCSTR does not cater to the UNICODE
define. LPCTSTR does.

That being said, Delphi has no real concept of the UNICODE define anyway.
Generated HPP files simply contain whatever output is produced by the Delphi
compiler. If the Delphi code uses Ansi data, so will the HPP file. Same
with Unicode data. On the C++ side, the use of char vs wchar_t for TCHAR
data is governed by the "TCHAR maps to" setting in the Project Options when
the C++ code is compiled, and so the UNICODE define only works wherever TCHAR
is used, but Delphi does not output TCHAR-based declarations in its output.

Your Delphi code should not be outputting Win32 API function declarations
in generated HPP files at all. They are already declared in the Win32 SDK
headers (in this case, in vfw.h), so you need to mark the functions with
{$EXTERNALSYM} on the Delphi side so they don't appear in generated HPPs, eg:

interface
 
uses
  Windows;
 
...
 
function AVIFileOpenA(var ppfile: PAVIFILE; szFile: LPCSTR; uMode: UINT; 
lpHandler: PCLSID): HResult; stdcall;
{$EXTERNALSYM AVIFileOpenA}
 
function AVIFileOpenW(var ppfile: PAVIFILE; szFile: LPCWSTR; uMode: UINT; 
lpHandler: PCLSID): HResult; stdcall;
{$EXTERNALSYM AVIFileOpenW}
 
function AVIFileOpen(var ppfile: PAVIFILE; szFile: LPCSTR; uMode: UINT; lpHandler: 
PCLSID): HResult; stdcall;
{$EXTERNALSYM AVIFileOpen}
 
implementation
 
function AVIFileOpenA; external AVIFILDLL;
function AVIFileOpenW; external AVIFILDLL;
function AVIFileOpen; external AVIFILDLL name 'AVIFileOpenA';


--
Remy Lebeau (TeamB)
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02