www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Calling a cpp function from d

reply "C2D" <null google.com> writes:
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
next sibling parent reply "John Chapman" <johnch_atms hotmail.com> writes:
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
parent reply "C2D" <null google.com> writes:
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*)
Jun 16 2015
next sibling parent reply "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> writes:
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.
Jun 16 2015
parent Daniel =?UTF-8?B?S296w6Fr?= via Digitalmars-d-learn writes:
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
Jun 16 2015
prev sibling next sibling parent "John Chapman" <johnch_atms hotmail.com> writes:
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.
Jun 17 2015
prev sibling parent "John Chapman" <johnch_atms hotmail.com> writes:
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)); }
Jun 17 2015
prev sibling next sibling parent Daniel =?UTF-8?B?S296w6Fr?= via Digitalmars-d-learn writes:
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
prev sibling parent reply "FreeSlave" <freeslave93 gmail.com> writes:
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
parent "FreeSlave" <freeslave93 gmail.com> writes:
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