Re: Marshall C++ string to C#
EG this https://docs.microsoft.com/en-us/windows/win32/api/winreg/nf-winreg-regqueryvalueexa?redirectedfrom=MSDN On Tue, Nov 26, 2019, 12:35 mike smith wrote: > Or one 'old world' technique some win APIs use > Pass in a null and the call tells you what size buffer you need, create it > and pass it in on a second call. > > > Mike > > On Tue, Nov 26, 2019, 11:53 Tony McGee wrote: > >> You could return a BSTR and let the interop marshaling do clean up for >> you: >> >> >> https://www.codeproject.com/Articles/1189085/Passing-strings-between-managed-and-unmanaged-code >> >> >> >> On Tue, 26 Nov. 2019, 11:09 Greg Keogh, wrote: >> >>> >>> msg is a stack variable, you can't return it iirc. Either create a var on the heap, or pass msg in as a parameter. >>> >>> D'oh! You're right. I changed the code to the following and it's >>> working. Notice use of CoTaskMemAlloc, which I think is the least worst >>> choice of heap allocation functions according to the MSDN docs. >>> >>> Notice that I've completely forgotten what the lengths are supposed to >>> be for string lengths, is it x2 for wchar? is it correct to allocate 200 >>> bytes for 100 wchars? The compiler is giving me an overrun warning. You're >>> supposed to use sizeof or something like that, but my brain has gone mushy >>> on all those rules now (which is part of the reason I never want to code >>> this assembler crap again!). >>> >>> Even worse … what about the memory leak in CoTaskMemAlloc? I make the >>> buffer, copy in the string and return it, but it's now leaked. Should I use >>> a global static buffer that can be released later? When? >>> >>> *GK* >>> >>> [image: image.png] >>> >>> >>>
Re: Marshall C++ string to C#
Or one 'old world' technique some win APIs use Pass in a null and the call tells you what size buffer you need, create it and pass it in on a second call. Mike On Tue, Nov 26, 2019, 11:53 Tony McGee wrote: > You could return a BSTR and let the interop marshaling do clean up for > you: > > > https://www.codeproject.com/Articles/1189085/Passing-strings-between-managed-and-unmanaged-code > > > > On Tue, 26 Nov. 2019, 11:09 Greg Keogh, wrote: > >> >> msg is a stack variable, you can't return it iirc. Either create a var on >>> the heap, or pass msg in as a parameter. >>> >> >> D'oh! You're right. I changed the code to the following and it's working. >> Notice use of CoTaskMemAlloc, which I think is the least worst choice of >> heap allocation functions according to the MSDN docs. >> >> Notice that I've completely forgotten what the lengths are supposed to be >> for string lengths, is it x2 for wchar? is it correct to allocate 200 bytes >> for 100 wchars? The compiler is giving me an overrun warning. You're >> supposed to use sizeof or something like that, but my brain has gone mushy >> on all those rules now (which is part of the reason I never want to code >> this assembler crap again!). >> >> Even worse … what about the memory leak in CoTaskMemAlloc? I make the >> buffer, copy in the string and return it, but it's now leaked. Should I use >> a global static buffer that can be released later? When? >> >> *GK* >> >> [image: image.png] >> >> >>
Re: Marshall C++ string to C#
You could return a BSTR and let the interop marshaling do clean up for you: https://www.codeproject.com/Articles/1189085/Passing-strings-between-managed-and-unmanaged-code On Tue, 26 Nov. 2019, 11:09 Greg Keogh, wrote: > > msg is a stack variable, you can't return it iirc. Either create a var on >> the heap, or pass msg in as a parameter. >> > > D'oh! You're right. I changed the code to the following and it's working. > Notice use of CoTaskMemAlloc, which I think is the least worst choice of > heap allocation functions according to the MSDN docs. > > Notice that I've completely forgotten what the lengths are supposed to be > for string lengths, is it x2 for wchar? is it correct to allocate 200 bytes > for 100 wchars? The compiler is giving me an overrun warning. You're > supposed to use sizeof or something like that, but my brain has gone mushy > on all those rules now (which is part of the reason I never want to code > this assembler crap again!). > > Even worse … what about the memory leak in CoTaskMemAlloc? I make the > buffer, copy in the string and return it, but it's now leaked. Should I use > a global static buffer that can be released later? When? > > *GK* > > [image: image.png] > > >
Re: Marshall C++ string to C#
> msg is a stack variable, you can't return it iirc. Either create a var on > the heap, or pass msg in as a parameter. > D'oh! You're right. I changed the code to the following and it's working. Notice use of CoTaskMemAlloc, which I think is the least worst choice of heap allocation functions according to the MSDN docs. Notice that I've completely forgotten what the lengths are supposed to be for string lengths, is it x2 for wchar? is it correct to allocate 200 bytes for 100 wchars? The compiler is giving me an overrun warning. You're supposed to use sizeof or something like that, but my brain has gone mushy on all those rules now (which is part of the reason I never want to code this assembler crap again!). Even worse … what about the memory leak in CoTaskMemAlloc? I make the buffer, copy in the string and return it, but it's now leaked. Should I use a global static buffer that can be released later? When? *GK* [image: image.png]
Re: Marshall C++ string to C#
msg is a stack variable, you can't return it iirc. Either create a var on the heap, or pass msg in as a parameter. Mike On Tue, Nov 26, 2019, 08:03 Greg Keogh wrote: > Folks, I have to make a quick demo of how a VS C++ coder can return a > Unicode string from a function back to a C# caller. Easy I thought... I > haven't written serious C++ since 2004 and things have changed for the > worse. The introduction of 'W' wide character support has blown everything > to the merdehouse. I don't even know which "string" to use, but I guess > that std:string (or std:wstring actually) is the preferred modern choice. > Then there are all the type aliases (wchar_t, LPCWSTR, etc). Then I find > many library functions (like localtime) are deprecated and cause compile > errors. The whole C++ language and ecosystem is now bigger than a > cyclotron. Anyway, I've pasted the code snippets here: > > [image: image.png] > > [image: image.png] > > Unfortunately all I get back is a garbage string full of \u > characters. I've tried dozens of combinations of changes everywhere, even > putting everything back to plain ASCII, but nothing will give me a good > string back from the function. > > Does anyone know for sure how to get a Unicode string back from C++? Web > searches produce hundreds of contradictory suggestions, all of which don't > compile, don't work or crash. > > Thanks > *Greg Keogh* >
Marshall C++ string to C#
Folks, I have to make a quick demo of how a VS C++ coder can return a Unicode string from a function back to a C# caller. Easy I thought... I haven't written serious C++ since 2004 and things have changed for the worse. The introduction of 'W' wide character support has blown everything to the merdehouse. I don't even know which "string" to use, but I guess that std:string (or std:wstring actually) is the preferred modern choice. Then there are all the type aliases (wchar_t, LPCWSTR, etc). Then I find many library functions (like localtime) are deprecated and cause compile errors. The whole C++ language and ecosystem is now bigger than a cyclotron. Anyway, I've pasted the code snippets here: [image: image.png] [image: image.png] Unfortunately all I get back is a garbage string full of \u characters. I've tried dozens of combinations of changes everywhere, even putting everything back to plain ASCII, but nothing will give me a good string back from the function. Does anyone know for sure how to get a Unicode string back from C++? Web searches produce hundreds of contradictory suggestions, all of which don't compile, don't work or crash. Thanks *Greg Keogh*