Thread overview | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
June 16, 2015 Calling a cpp function from d | ||||
---|---|---|---|---|
| ||||
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? |
June 16, 2015 Re: Calling a cpp function from d | ||||
---|---|---|---|---|
| ||||
Posted in reply to C2D | On Tuesday, 16 June 2015 at 12:26:45 UTC, C2D wrote:
> BOOL result = SHGetFolderPath(null, 0x23, null, 0, cache.ptr);
That should be:
BOOL result = SHGetFolderPath(null, 0x23, null, 0, buf.ptr);
|
June 16, 2015 Re: Calling a cpp function from d | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Chapman | On Tuesday, 16 June 2015 at 12:31:23 UTC, John Chapman wrote:
> On Tuesday, 16 June 2015 at 12:26:45 UTC, C2D wrote:
>> BOOL result = SHGetFolderPath(null, 0x23, null, 0, cache.ptr);
>
> That should be:
>
> BOOL result = SHGetFolderPath(null, 0x23, null, 0, buf.ptr);
Thanks, but I'm still getting the same 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, wchar*)
|
June 16, 2015 Re: Calling a cpp function from d | ||||
---|---|---|---|---|
| ||||
Posted in reply to C2D | On Tuesday, 16 June 2015 at 12:42:16 UTC, C2D wrote:
> On Tuesday, 16 June 2015 at 12:31:23 UTC, John Chapman wrote:
>> On Tuesday, 16 June 2015 at 12:26:45 UTC, C2D wrote:
>>> BOOL result = SHGetFolderPath(null, 0x23, null, 0, cache.ptr);
>>
>> That should be:
>>
>> BOOL result = SHGetFolderPath(null, 0x23, null, 0, buf.ptr);
>
> Thanks, but I'm still getting the same 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, wchar*)
Because you have declared `buf` as `wchar[...]`. SHGetFolderPath expects `char*`, so use `char[...]` instead.
|
June 16, 2015 Re: Calling a cpp function from d | ||||
---|---|---|---|---|
| ||||
Posted in reply to C2D | On Tue, 16 Jun 2015 12:26:45 +0000 C2D via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> 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? Something like this should work import std.string : fromStringz; import std.traits : PointerTarget; auto buf = new PointerTarget!LPTSTR[MAX_PATH + 2]; auto result = SHGetFolderPath(null, 0x23, null, 0, buf.ptr); return fromStringz(buf); |
June 16, 2015 Re: Calling a cpp function from d | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marc Schütz | On Tue, 16 Jun 2015 13:01:09 +0000 via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> wrote: > On Tuesday, 16 June 2015 at 12:42:16 UTC, C2D wrote: > > On Tuesday, 16 June 2015 at 12:31:23 UTC, John Chapman wrote: > >> On Tuesday, 16 June 2015 at 12:26:45 UTC, C2D wrote: > >>> BOOL result = SHGetFolderPath(null, 0x23, null, 0, cache.ptr); > >> > >> That should be: > >> > >> BOOL result = SHGetFolderPath(null, 0x23, null, 0, buf.ptr); > > > > Thanks, but I'm still getting the same 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, wchar*) > > Because you have declared `buf` as `wchar[...]`. SHGetFolderPath expects `char*`, so use `char[...]` instead. That is not wise, it could be a wchar. http://stackoverflow.com/questions/321413/lpcstr-lpctstr-and-lptstr |
June 16, 2015 Re: Calling a cpp function from d | ||||
---|---|---|---|---|
| ||||
Posted in reply to C2D | 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 |
June 16, 2015 Re: Calling a cpp function from d | ||||
---|---|---|---|---|
| ||||
Posted in reply to FreeSlave | On Tuesday, 16 June 2015 at 13:31:47 UTC, FreeSlave wrote:
> 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).
Note that I load shell32 dynamically, so no need to link it while building. Even if dmd links to shell32 by default, I'm not sure if this is true for gdc and ldc (never used them on win). At least, as I remember Mingw does not link to shell32 by default.
|
June 17, 2015 Re: Calling a cpp function from d | ||||
---|---|---|---|---|
| ||||
Posted in reply to C2D | On Tuesday, 16 June 2015 at 12:42:16 UTC, C2D wrote:
> On Tuesday, 16 June 2015 at 12:31:23 UTC, John Chapman wrote:
>> On Tuesday, 16 June 2015 at 12:26:45 UTC, C2D wrote:
>>> BOOL result = SHGetFolderPath(null, 0x23, null, 0, cache.ptr);
>>
>> That should be:
>>
>> BOOL result = SHGetFolderPath(null, 0x23, null, 0, buf.ptr);
>
> Thanks, but I'm still getting the same error -
Use SHGetFolderPathW.
|
June 17, 2015 Re: Calling a cpp function from d | ||||
---|---|---|---|---|
| ||||
Posted in reply to C2D | On Tuesday, 16 June 2015 at 12:42:16 UTC, C2D wrote:
> On Tuesday, 16 June 2015 at 12:31:23 UTC, John Chapman wrote:
>> On Tuesday, 16 June 2015 at 12:26:45 UTC, C2D wrote:
>>> BOOL result = SHGetFolderPath(null, 0x23, null, 0, cache.ptr);
>>
>> That should be:
>>
>> BOOL result = SHGetFolderPath(null, 0x23, null, 0, buf.ptr);
>
> Thanks, but I'm still getting the same error -
Here's a working version:
import core.sys.windows.windows;
extern(Windows)
HRESULT SHGetFolderPathW(HWND hwndOwner, int nFolder, HANDLE hToken, DWORD dwFlags, LPWSTR pszPath);
string getFolderPath(int folder) {
import core.stdc.wchar_ : wcslen;
import std.utf : toUTF8;
wchar[MAX_PATH] buffer;
if (SHGetFolderPathW(null, folder, null, 0, buffer.ptr) >= 0)
return buffer[0 .. wcslen(buffer.ptr)].toUTF8();
return null;
}
void main() {
writeln(getFolderPath(0x23));
}
|
Copyright © 1999-2021 by the D Language Foundation