digitalmars.D.learn - Calling a cpp function from d
- C2D (20/20) Jun 16 2015 Hi,
- John Chapman (3/4) Jun 16 2015 That should be:
- C2D (6/10) Jun 16 2015 Thanks, but I'm still getting the same error -
- "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> (3/15) Jun 16 2015 Because you have declared `buf` as `wchar[...]`. SHGetFolderPath
- Daniel =?UTF-8?B?S296w6Fr?= via Digitalmars-d-learn (4/21) Jun 16 2015 That is not wise, it could be a wchar.
- John Chapman (2/10) Jun 17 2015 Use SHGetFolderPathW.
- John Chapman (17/25) Jun 17 2015 Here's a working version:
- Daniel =?UTF-8?B?S296w6Fr?= via Digitalmars-d-learn (8/34) Jun 16 2015 Something like this should work
- FreeSlave (20/40) Jun 16 2015 I don't know what binding to shell32.dll you use. Probably you
- FreeSlave (5/9) Jun 16 2015 Note that I load shell32 dynamically, so no need to link it while
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?
Jun 16 2015
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);
Jun 16 2015
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: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*)BOOL result = SHGetFolderPath(null, 0x23, null, 0, cache.ptr);That should be: BOOL result = SHGetFolderPath(null, 0x23, null, 0, buf.ptr);
Jun 16 2015
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:Because you have declared `buf` as `wchar[...]`. SHGetFolderPath expects `char*`, so use `char[...]` instead.On Tuesday, 16 June 2015 at 12:26:45 UTC, C2D wrote: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*)BOOL result = SHGetFolderPath(null, 0x23, null, 0, cache.ptr);That should be: BOOL result = SHGetFolderPath(null, 0x23, null, 0, buf.ptr);
Jun 16 2015
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:That is not wise, it could be a wchar. http://stackoverflow.com/questions/321413/lpcstr-lpctstr-and-lptstrOn Tuesday, 16 June 2015 at 12:31:23 UTC, John Chapman wrote:Because you have declared `buf` as `wchar[...]`. SHGetFolderPath expects `char*`, so use `char[...]` instead.On Tuesday, 16 June 2015 at 12:26:45 UTC, C2D wrote: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*)BOOL result = SHGetFolderPath(null, 0x23, null, 0, cache.ptr);That should be: BOOL result = SHGetFolderPath(null, 0x23, null, 0, buf.ptr);
Jun 16 2015
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:Use SHGetFolderPathW.On Tuesday, 16 June 2015 at 12:26:45 UTC, C2D wrote:Thanks, but I'm still getting the same error -BOOL result = SHGetFolderPath(null, 0x23, null, 0, cache.ptr);That should be: BOOL result = SHGetFolderPath(null, 0x23, null, 0, buf.ptr);
Jun 17 2015
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: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)); }On Tuesday, 16 June 2015 at 12:26:45 UTC, C2D wrote:Thanks, but I'm still getting the same error -BOOL result = SHGetFolderPath(null, 0x23, null, 0, cache.ptr);That should be: BOOL result = SHGetFolderPath(null, 0x23, null, 0, buf.ptr);
Jun 17 2015
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);
Jun 16 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
Jun 16 2015
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.
Jun 16 2015