Calling a cpp function from d

FreeSlave via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jun 16 06:31:45 PDT 2015


On Tuesday, 16 June 2015 at 12:26:45 UTC, C2D wrote:
> Hi,
> I encountered the following error:
>
> Error: function files.SHGetFolderPath (void* hwndOwner, int 
> nFolder, void* hToken, uint dwFlags, char* pszPath) is not 
> callable using argument types (typeof(null), int, typeof(null), 
> int, immutable(char)*)
>
> When I'm try to run this code:
> ...
> {
> import std.utf : toUTF8;
> static string cache;
>
> wchar[MAX_PATH + 2] buf;
>
> BOOL result = SHGetFolderPath(null, 0x23, null, 0, cache.ptr);
>
> return cache;
> }
> ...
> extern(Windows) HRESULT SHGetFolderPath(HWND hwndOwner, int 
> nFolder, HANDLE hToken, DWORD dwFlags, LPTSTR pszPath);
>
> I tried everything I know about D and CPP. What can be the 
> solution?

I don't know what binding to shell32.dll you use. Probably you 
need set -version=Unicode to compiler flags, so WinAPI aliases 
refer to W variants of functions.

Personally I prefer to call explicitly W or A variants of WinAPI 
functions.
Therefore declaration should look like this:

extern(Windows) HRESULT SHGetFolderPathW(HWND hwndOwner, int 
nFolder, HANDLE hToken, DWORD dwFlags, wchar* pszPath)

Or even better

extern(Windows) @nogc @system HRESULT SHGetFolderPathW(HWND 
hwndOwner, int nFolder, HANDLE hToken, DWORD dwFlags, wchar* 
pszPath) nothrow

to allow using this function in %nogs nothrow code.

Also you may want to take a look at my library [1] where I use 
SHGetSpecialFolderPath (I know, it's deprecated, but it still 
works, so why not. I don't really need to bother with access 
tokens here).


[1] 
https://github.com/MyLittleRobo/standardpaths/blob/master/source/standardpaths.d#L299


More information about the Digitalmars-d-learn mailing list