Marshall char** to string problem calling unmanaged code from managed code - c++

I've this C++ function,
bool MyClass::my_function(int num, TCHAR** filepath)
I've expose the function as
extern "C"
{
__declspec(dllexport) bool MyFunction(int num, char* filepath[])
{
OutputDebugStringA("--MyFunction--");
TCHAR **w_filepath = (TCHAR **)calloc(num, 2* sizeof(TCHAR **));
for(int i = 0; i < num; i++)
{
OutputDebugStringA(filepath[i]);
int len = strlen(filepath[i]) + 1;
w_filepath[i] = (TCHAR *)calloc (1, len);
ctow(w_filepath[i], filepath[i]); // converts char to WCHAR
}
bool ret = MyClass.my_function(num, w_filepath);
OutputDebugStringA("End -- MyFunction --");
free(w_filepath);
return ret;
}
}
I've C# wrapper as
[DllImport("MyDll.dll")]
public static extern bool MyFunction(int num, [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPTStr)] string[] filepath);
In C#, i call Myfunction as
string [] filepath = { "D:\\samplefile\\abc.txt", "D:\\samplefile\\def.txt"}
MyFunction(2, filepath)
In C++ function, it gets only first character of filepath.
For example, from above call if i print in C++ code using
OutputDebugStringA
it prints only D for both first and second.
If i remove
[MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPTStr)]
from c# wrapper
I'll get Access violation error in
w_filepath[i] = (TCHAR *)calloc (1, len)
for second file.
Please help me.

1) w_filepath[i] = (TCHAR *)calloc (1, len); - calloc requires size in bytes, so it should be w_filepath[i] = (wchar_t *)calloc (1, len*sizeof(wchar_t));
2) data from c# comes as wchar_t*, so you don't need converting routines at all , and should change function declaration to
__declspec(dllexport) bool MyFunction(int num, wchar_t* filepath[])

Related

How concatenate a char with TCHAR array?

I want concatenate a random string with a directory name and the final result must be something like this:
C:\Program Files (x86)\AAAFFF1334
On following code this part: "AAAFFF1334" comes strange characters see:
What must be made to fix this?
TCHAR mydir[MAX_PATH];
void gen_random(char *s, const int len) {
static const char alphanum[] =
"0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
for (int i = 0; i < len; ++i) {
s[i] = alphanum[rand() % (sizeof(alphanum)-1)];
}
s[len] = 0;
}
// main
TCHAR szProgramFiles[MAX_PATH];
HRESULT hProgramFiles = SHGetFolderPath(NULL, CSIDL_PROGRAM_FILES, NULL, SHGFP_TYPE_CURRENT, szProgramFiles);
char str;
gen_random(&str, 10);
wsprintf(mydir, TEXT("%s\\%s"), szProgramFiles, str);
gen_random should get char array with at least 11 characters (10 for size + 1 for terminating null).
So it should be:
char str[10+1]; //or char str[11];
gen_random(str, 10);
in addition, the format string should be: "%s\\%hs", the first is TCHAR* type (if UNICODE defined wchar_t* if not char*) the second is always char* type.
hs, hS
String. This value is always interpreted as type LPSTR, even
when the calling application defines Unicode.
look here
Note: in Microsoft documentation:
LPSTR = always char*
LPWSTR = always wchar_t*
LPTSTR = TCHAR* (if UNICODE defined: wchar_t*, else: char*)

Convert unsigned long long to wchar_t * and concatenate

There are tons of questions on this issue and I have been attempting the various solutions. There seems to be dozens of ways to do this however none of them are working. I am very new to C++ and VS, working for about a month, and I am trying to code an auto Excel program using VC++. I am stuck trying to concatenate a wchar_t * and an unsigned long long. I assume the first step is to "convert" the unsigned long long to wchar_t *. I apologize for throwing in the whole code but I think it may help with showing what I am aiming for and if there are any other weaknesses in the code.
wchar_t * ex(wchar_t * dest, unsigned long long num);
int main()
{
unsigned long long num = 10;
wchar_t *dest= L"A2:B";
wchar_t * Path=ex(dest, num);
VARIANT param;
param.vt = VT_BSTR;
// param.bstrVal = SysAllocString(L"A2:B10");
param.bstrVal = SysAllocString(Path);
getchar();
return 0;
}
wchar_t * ex(wchar_t * dest, unsigned long long num)
{
// Convert num to wchar_t *
wchar_t *rangeMax = (wchar_t *)num;
// I think this is used to eliminate extra space in other solutions
// but not here. It could be useful.
const int MAX_CHARS = 50;
size_t count = wcsnlen_s(dest, MAX_CHARS);
wprintf(L"The length of the string is %ld characters\n", count);
// Throw dest into buf
wchar_t buf[25] = { 0 };
int r = wcscpy_s(buf, 25, dest);
if (r != 0) {
wprintf(L"wcscpy_s() failed %ld", r);
}
r = wcscat_s(buf, 25, rangeMax);
if (r != 0) {
wprintf(L"wcscat_s() failed %ld", r);
}
wprintf_s(buf);
return buf;
}
ex is an edited example from zetcode. I think it is close to being the solution, however when combining buf and rangeMax the code throws all sorts of memory exceptions and fails.
As you can see the final destination for the concatenated wchar_t * is as a BSTR in a VARIANT through SysAllocString.
I appreciate any suggestions on code improvement as well as how to make the code actually run!
As suggested using wstring functioned as intended. Thank you for pointing out I was returning a pointer to a local variable! Once back in main the type was changed to wchar_t * which passed nicely to SysAllocString() for use with my main program.
std::wstring ex(wchar_t * dest, unsigned long long num);
int main()
{
unsigned long long num = 10;
wchar_t *dest= L"A2:B";
std::wstring PathString= ex(dest, num);
wchar_t *wPath = (WCHAR *)PathString.c_str();
std::wcout << L"In main\n";
std::wcout << wPath << L'\n';
VARIANT param;
param.vt = VT_BSTR;
//param.bstrVal = SysAllocString(L"A2:B10");
param.bstrVal = SysAllocString(wPath);
getchar();
return 0;
}
std::wstring ex(wchar_t * dest, unsigned long long num)
{
std::wstring rangeMax = std::to_wstring(num);
std::wstring string(dest);
string += rangeMax;
std::wcout << L"In function\n";
std::wcout<<string<<L'\n';
return string;
}

Keep a string value in memory after "FreeLibrary"

I do have a simple test-case here (C++) which does LoadLibrary, GetProcAddress & calls the function (ProcAdd).
The signature is "char* ProcAdd(char*)".
Now I do get the string correctly in the executable, but once I do "FreeLibrary", it's gone... (obviously because I just did a return "hello").
The thing is, that I have another dll (.NET, C#) where the signature is "[return: MarshalAs LPSTR]string ProcAdd([MarshalAs LPSTR] string)".
Now this function ALSO returns a string, but when I do "FreeLibrary", the string is still accessible within my executable?!
How does that come, and how could I mimic the same behaviour?
(and yes, I know I can store it in another variable, but I would like to understand what is happening and how I can reproduce this .NET behaviour).
Thanks a lot!
As requested the code:
C++ exe:
int main( void )
{
HINSTANCE hinstLib;
MYPROC ProcAdd;
BOOL fFreeResult, fRunTimeLinkSuccess = FALSE;
void * val = NULL;
// Get a handle to the DLL module.
// hinstLib = LoadLibrary(TEXT("C:\\Users\\steven\\temp\\myMyMy.orig.dll"));
hinstLib = LoadLibrary(TEXT("C:\\Users\\steven\\temp\\myMyMy.proxy.dll"));
// If the handle is valid, try to get the function address.
if (hinstLib != NULL)
{
ProcAdd = (MYPROC) GetProcAddress(hinstLib, "ProcAdd");
// If the function address is valid, call the function.
if (NULL != ProcAdd)
{
fRunTimeLinkSuccess = TRUE;
val = (ProcAdd) ("0987654321");
}
// Free the DLL module.
fFreeResult = FreeLibrary(hinstLib);
}
// If unable to call the DLL function, use an alternative.
if (! fRunTimeLinkSuccess)
printf("Message printed from executable\n");
return 0;
}
C++ dll:
#include <Windows.h>
#include <iostream>
#include <fstream>
static const char tmp[] = "hello";
extern "C" const char * __stdcall ProcAdd(const char * param1) {
FILE * fp = fopen("C:\\tmp\\ProcAdd.txt", "a");
if ( fp ) {
fprintf(fp, "param1: '%s'\r\n", param1);
fclose(fp);
}
// return strdup("hello");
// return "hello";
return tmp;
}
C# dll:
[return: MarshalAs(UnmanagedType.LPStr)]
public static string ProcAdd([MarshalAs(UnmanagedType.LPStr)] string param1)
{
string str;
try
{
str = new WebClient().DownloadString("http://www.salvania.be/test.php?param1=" + param1);
}
catch (Exception exception1)
{
str = "Error-DLL";
}
return str;
}
Working return:
// http://stackoverflow.com/questions/14406818/heapcreate-and-heapalloc-confuse
HANDLE winHandle = HeapCreate( 0, sizeof(tmp), sizeof(tmp) );
char* s = (char*)HeapAlloc( winHandle, 0, sizeof(tmp) + 1 );
strcpy((char*)s, tmp);
s[sizeof(tmp)] = 0;
If you just return "hello", that hello string may come from the data segment of the shared library, and may be unloaded after the life-time of the library.
To ensure the returned string live after the life-time of the library, you may store it on heap or otherwise provide a buffer from the caller.

overloading new and delete in c++

HI All,
I was trying to overload new and delete to fix a memory leak problem in my project. But got stuck with some compilation error.
Currently this code is bit shabby
Here is my hdr file
#include <cstddef>
#include <iostream>
#include <list>
#include <stdarg.h>
#include <stdio.h>
using namespace std;
typedef unsigned int DWORD;
void AddTrack(DWORD addr, DWORD asize, const char *fname, DWORD lnum);
char *OutputDebugString (const char *fmt, ...);
void RemoveTrack(DWORD addr);
void DumpUnfreed();
#ifdef _DEBUG
#define DEBUG_NEW new(__FILE__, __LINE__)
#define new DEBUG_NEW
void * operator new (unsigned int size, const char *file, int line)
{
void *ptr = (void *)malloc(size);
AddTrack((DWORD)ptr, size, file, line);
return(ptr);
}
/*inline void * operator new(unsigned int size)
{
void *ptr = (void *)malloc(size);
AddTrack((DWORD)ptr, size, _FILE_,_LINE_);
return(ptr);
}*/
void operator delete(void *p)
{
RemoveTrack((DWORD)p);
free(p);
}
#endif
char *OutputDebugString (const char *fmt, ...)
{
char *p = NULL;
size_t size = 1024;
int n = 0;
va_list ap;
if((p = (char*) malloc(size)) == NULL)
return NULL;
while(1) {
va_start(ap, fmt);
n = vsnprintf(p, size, fmt, ap);
va_end(ap);
if(n > -1 && n < size)
return p;
/* failed: have to try again, alloc more mem. */
if(n > -1) /* glibc 2.1 */
size = n + 1;
else /* glibc 2.0 */
size *= 2; /* twice the old size */
if((p = (char *)realloc (p, size)) == NULL)
return NULL;
}
}
typedef struct information {
DWORD address;
DWORD size;
char file[64];
DWORD line;
} ALLOC_INFO;
typedef list < ALLOC_INFO* > AllocList;
AllocList *allocList;
void AddTrack(DWORD addr, DWORD asize, const char *fname, DWORD lnum)
{
ALLOC_INFO *info;
if(!allocList) {
//allocList = new AllocList;
allocList = (AllocList*)malloc (sizeof (AllocList));
}
//info = new(ALLOC_INFO);
info = (ALLOC_INFO*) malloc (sizeof (ALLOC_INFO));
info->address = addr;
strncpy(info->file, fname, 63);
info->line = lnum;
info->size = asize;
allocList->insert(allocList->begin(), info);
}
void RemoveTrack(DWORD addr)
{
AllocList::iterator i;
if(!allocList)
if(!allocList)
return;
for(i = allocList->begin(); i != allocList->end(); i++)
{
if((*i)->address == addr)
{
allocList->remove((*i));
break;
}
}
}
void DumpUnfreed()
{
AllocList::iterator i;
DWORD totalSize = 0;
char buf[1024];
if(!allocList)
return;
for(i = allocList->begin(); i != allocList->end(); i++) {
sprintf(buf, "%-50s:\t\tLINE %d,\t\tADDRESS %d\t%d unfreed\n",
(*i)->file, (*i)->line, (*i)->address, (*i)->size);
OutputDebugString("%s",buf);
totalSize += (*i)->size;
}
sprintf(buf, "-----------------------------------------------------------\n");
OutputDebugString("%s",buf);
sprintf(buf, "Total Unfreed: %d bytes\n", totalSize);
OutputDebugString("%s",buf);
}
And my main.cpp is
#include "mynew.h"
int main()
{
char *ptr = new char;
DumpUnfreed();
return 0;
}
When i try to compile i get the following error
[root#dhcppc0 new]# !g
g++ main.cpp -D_DEBUG
mynew.h:25: error: declaration of ‘operator new’ as non-function
main.cpp: In function ‘int main()’:
main.cpp:9: error: no matching function for call to ‘operator new(unsigned int, const char [9], int)’
/usr/lib/gcc/i386-redhat-linux/4.1.1/../../../../include/c++/4.1.1/new:84: note: candidates are: void* operator new(size_t)
/usr/lib/gcc/i386-redhat-linux/4.1.1/../../../../include/c++/4.1.1/new:88: note: void* operator new(size_t, const std::nothrow_t&)
/usr/lib/gcc/i386-redhat-linux/4.1.1/../../../../include/c++/4.1.1/new:94: note: void* operator new(size_t, void*)
I know there is some thing wrong with my #defines, but I am not sure what is wrong.
Can any one please bale me out of this
You've defined your new macro before your functions. Your code ends up looking like:
void *
operator new(__FILE__, __LINE__)(unsigned int size, const char *file, int line)
Which is obviously wrong. Your should move the macro definitions underneath the functions (or better is to keep those functions in a .cpp file you link with.) For what it's worth, new is a keyword and cannot be an identifier, so your program is, strictly speaking, ill-formed.
I recently posted my global memory operators framework. It might help you a bit.
the signature don't match it sould be void* operator new (size_t size).
overriding single object new signature is
static void * operator new(site_t size),
roni

How do I convert from _TCHAR * to char * when using C++ variable-length args?

We need to pass a format _TCHAR * string, and a number of char * strings into a function with variable-length args:
inline void FooBar(const _TCHAR *szFmt, const char *cArgs, ...) {
//...
}
So it can be called like so:
char *foo = "foo";
char *bar = "bar";
LogToFileA(_T("Test %s %s"), foo, bar);
Obviously a simple fix would be to use _TCHAR instead of char, but we don't have that luxury unfortunately.
We need to use this with va_start, etc so we can format a string:
va_list args;
_TCHAR szBuf[BUFFER_MED_SIZE];
va_start(args, cArgs);
_vstprintf_s(szBuf, BUFFER_MED_SIZE, szFmt, args);
va_end(args);
Unfortunately we cannot use this because it give us this error:
Unhandled exception at 0x6a0d7f4f (msvcr90d.dll) in foobar.exe:
0xC0000005: Access violation reading location 0x2d86fead.
I'm thinking we need to convert our char * to _TCHAR * - but how?
Use %hs or %hS instead of %s. That will force the parameters to be interpretted as char* in both Ansi and Unicode versions of printf()-style functions, ie:
inline void LogToFile(const _TCHAR *szFmt, ...)
{
va_list args;
TCHAR szBuf[BUFFER_MED_SIZE];
va_start(args, szFmt);
_vstprintf_s(szBuf, BUFFER_MED_SIZE, szFmt, args);
va_end(args);
}
{
char *foo = "foo";
char *bar = "bar";
LogToFile(_T("Test %hs %hs"), foo, bar);
}
Usually it looks like the following:
char *foo = "foo";
char *bar = "bar";
#ifdef UNICODE
LogToFileW( L"Test %S %S", foo, bar); // big S
#else
LogToFileA( "Test %s %s", foo, bar);
#endif
Your question is not completely clear. How your function is implemented and how do you use it?
Here was my solution - I welcome suggestions for improvement!
inline void FooBar(const _TCHAR *szFmt, const char *cArgs, ...) {
va_list args;
_TCHAR szBuf[BUFFER_MED_SIZE];
// Count the number of arguments in the format string.
const _TCHAR *at = _tcschr(szFmt, '%');
int argCount = 0;
while(at) {
argCount++;
at = _tcschr(at + 1, '%');
}
CA2W *ca2wArr[100];
LPWSTR szArgs[100];
va_start(args, cArgs);
for (int i = 1; i < argCount + 1; i++) {
CA2W *ca2w = new CA2W(cArgs);
szArgs[i] = ca2w->m_psz;
ca2wArr[i] = ca2w;
cArgs = va_arg(args, const char *);
}
va_end(args);
// Use the new array we just created (skips over first element).
va_start(args, szArgs[0]);
_vstprintf_s(szBuf, BUFFER_MED_SIZE, szFmt, args);
va_end(args);
// Free up memory used by CA2W objects.
for (int i = 1; i < argCount + 1; i++) {
delete ca2wArr[i];
}
// ... snip ... - code that uses szBuf
}
this is something I have used before to convert a TCHAR to char, hope it helps, although I wasn't really looking for optimization, so it's not the fastest way.. but it worked!
TCHAR tmp[255];
::GetWindowText(hwnd, tmp, 255);
std::wstring s = tmp;
//convert from wchar to char
const wchar_t* wstr = s.c_str();
size_t wlen = wcslen(wstr) + 1;
char newchar[100];
size_t convertedChars = 0;
wcstombs_s(&convertedChars, newchar, wlen, wstr, _TRUNCATE);