Easiest way to pass modifiable System.String as modifiable LPTSTR?

BOOL PathFindOnPath (LPTSTR pszFile, LPCTSTR * ppszOtherDirs);

I am calling this API from managed C ++. My pszFile is located in System.String.

What is the easiest way to pass this as LPTSTR? (Given its non-input parameter)

I tried pin_ptr and inner_ptr, but none of them were accepted by the compiler.

+3
source share
2 answers

You need to marshal the (pre-allocated) StringBuilder instead of a String reference. For more information, see the MSDN Article at Marshaling .

+3
source

, , . , , . , . , , , . :

#include <vcclr.h>
...
    String^ file = "blah.txt";
    wchar_t path[_MAX_PATH];
    {
        pin_ptr<const wchar_t> wch = PtrToStringChars(file);
        wcscpy_s(path, _MAX_PATH, wch);
    }
    BOOL ok = PathFindOnPath(path, something);

, , , .

+1

Source: https://habr.com/ru/post/1751068/


All Articles