How to transfer byte[] from C# to char buffer in C/ C++? - c++

I have byte[] in C#. I want to pass this value to C++ (CLR). How to do it ?
I have tried converting the byte[][] to char[][] in C#.
Now Is it possible to use this char** variable in C++? If it is not, what I need to use ?
EDIT:
I want the output in character buffer in C++ code.
My Existing code can be found at Passing objects between C# library and C++ (CLR)

Related

Easiest way to convert UnicodeString to const char* in c++?

I'm new in c++ and have problem with converting UnicodeString to string, so now searching for easiest method to convert from one type to other.
I want to use basic windows function which needs string with UnicodeString, how to make code work?
UnicodeString Exec = "notepad";
WinExec(Exec.c_str(), 0);
Environment used is c++ builder xe2
A std::string can not store unicode data. You will need a std::wstring for that.
I've never heard of UnicodeString before, but looking at the API here:
http://docwiki.embarcadero.com/Libraries/XE2/en/System.UnicodeString_Methods
It has a function called .c_str() which returns a wchar_t* which you can then use to construct a std::wstring
If you really need a std::string, then have a look at this answer.
How to convert wstring into string?
If you are looking for complete unicode support in C++ go for ICU API. Here is the website where you can find everything about it. http://site.icu-project.org/

Use C++ array directly in Java API requiring byte[]

As it stands, I have a C++ app that has a unsigned char* buffer containing PCM audio data. I need to call the Android API method AudioTrack.write() on an instance of AudioTrack over JNI (from C++ to Java) with this data, and I would like to avoid making an extra copy in doing so. Can I do this?
AudioTrack accepts as one of its arguments a Java byte[], the argument that should correspond to my PCM data (unsigned char*).
Sorry if this is a duplicate... it's hard to effectively search for this kind of thing.
Something like this should do.
I haven't compiled this, and it would be wise to check the syntax also with the specs.
jbyteArray byteArray;
byteArray = env->NewByteArray(audioDataLength);
env->SetByteArrayRegion(byteArray, 0, audioDataLength , (jbyte*) audioData);
Where audioDataLength is the length of the char* audioData

How do you convert C++ _tcscpy, _tcscat to Delphi?

I'm converting this code from C++ to Delphi but I don't get the following part of the code. Can anyone explain me what the following code means; what's happening to the szBuff buffer ?
I'm pretty sure it's such kind of formatting (replacement), but I don't even know what is expected as a result and I can't find any sensible documentation of the used functions (maybe I'm just a lame :)
Can anyone help me with the translation of this code to Delphi (or direct me to proper documentation) ?
I don't like this how do you convert kind of questions by myself, so I mentioned at least function names in the question title so it might searchable to someone else in the future.
function TSecInfo.BuildSecurityAttributes(var SecAttrs: TSecurityAttributes): boolean;
var
pszSidUser: PChar;
szBuff: array [0..1024] of Char;
begin
// pszSidUser at this time contains user SID like this
// S-1-5-21-1454471165-1004336348-1606980848-5555
// TCHAR szBuff[1024]; // I'm not sure with array [0..1024] of Char;
_tcscpy(szBuff, _T("D:"));
_tcscat(szBuff, _T("(A;;GA;;;"));
_tcscat(szBuff, pszSidUser);
_tcscat(szBuff, _T(")"));
_tcscat(szBuff, _T("(A;;GWGR;;;AN)"));
_tcscat(szBuff, _T("(A;;GWGR;;;WD)"));
...
_tcscat(szBuff, _T("S:(ML;;NW;;;S-1-16-0)"));
end;
For those who are interested in what's the whole code from the link about I can tell it should be a trick how to access network pipes for writing as an anonymous user on Windows Vista above. To the whole article follow this link.
Thanks for your time
Regards
_tcscpy and _tcscat are TCHAR macro versions of C standard library functions strcpy and strcat for copying and concatenating C strings. They evaluate to ANSI or Unicode versions depending on whether or the type of project you are targeting. It's really C code rather than C++ code in my view.
In Delphi you would simply use string variables like this:
function TSecInfo.BuildSecurityAttributes(var SecAttrs: TSecurityAttributes): boolean;
var
pszSidUser: PChar;
Buff: string;
begin
// pszSidUser at this time contains user SID like this
// S-1-5-21-1454471165-1004336348-1606980848-5555
Buff := 'D:(A;;GA;;;'+pszSidUser+')(A;;GWGR;;;AN)(A;;GWGR;;;WD)S:(ML;;NW;;;S-1-16-0)';
SomeOtherWindowsAPICall(PChar(Buff));
end;
Presumably in the C code there is a call to another Windows API function that receives an LPCTSTR. The C code will pass szBuff but you can simply pass PChar(Buff) as I have shown above.
The C code is using a fixed length buffer because it doesn't have available a dynamically allocated string class like Delphi's string or std::string in C++. Fixed length buffers like this often lead to buffer overruns. In Delphi don't use a fixed length buffer if you can avoid it.
This is a classic example of why languages with built in string handling are so much easier to work with than C.
It looks like the code is using TCHARS, basically they are a macro which makes going from unicode to non-unicode easier. _tcscpy is copying the parameter to szBuff, _tcscat is appending the parameter to szBuff. If you are familar with strcpy and strcat they do the same thing.
_tcscpy(szBuff, _T("D:")); //szBuff == "D:"
_tcscat(szBuff, _T("(A;;GA;;;")); //szBuff == "D:A;;GA;;;"
...

Pass BSTR from C++ to C#

I have C# project "X" ,I have exposed methods in it to the C++ project "Y".
X has method signature as follows -
public void WriteInformation(string sInfo)
{
m_logger.ErrorInfo("{0}", sInfo);
}
As I am exporting it to a C++ using .TLB file I checked declaration of this method in .tlh file which generates on #import of .tlb file.
virtual HRESULT __stdcall WriteInformation ( /*[in]*/ BSTR sMsg ) = 0;
I am calling this method in C++ project and passsing argument as follows -
oLog->WriteInformation(BSTR("Info write successful"));
Issue here is the string passed from C++ always becomes garbage or null , I debugged it and I can see value of sInfo is always garbage or null.
Please let me know what method should be followed to pass string from C++ to C#.
You try to pass an ANSI string in place of BSTR. BSTR must be a wide character string. Also you shouldn't pass a string literal, you should properly allocate a BSTR using SysAllocString() (or better yet) a wrapper class like ATL::CComBSTR or _bstr_t. Btw _bstr_t has a constructor that will accept const char* and do the ANSI->UTF16 conversion for you.
I dont think its possible to interact with C++ and C# directly. I had interacted using a C++/CLI wrapper.

mapping user defined data type (VBA) to C++

Can somebody please tell me how to pass a VBA user defined type to a c++ (dll). I specially need to know how to handle string type of VBA in the dll.
Thank You
Variable-length string members are passed as BSTR. Fixed-length string members are passed as arrays of CHAR containing ANSI strings.
There is an exhaustive sample in the Microsoft documentation here. This was originally written for VB5 but it also applies to VBA.