Link to home
Start Free TrialLog in
Avatar of Develprog
Develprog

asked on

Passing argument parameters using __targv[x] with unicode

Hi,

What the trouble is with this code when arguments function (argc , argv ) is used in a UNICODE project. I set in EDI VC6 parameter settings "11.1.1.111 10 the_name"

      else if (__argc==4)  //with 3 parametres (@IP + fps + name)
      {
            CString title = pCVgtLanguage->vgt_translate (&ppCLang, L"000001",L"FXCAMD Configuratorfr :");
            CString space = L"   ";
            CString tempo1 = __targv[1];  // here problem tempo1 contain is "1" isntead of "11.1.1.111"
            CString str_fps = __targv[2];
            WCHAR* c = L"\0";
            gl_str_int = wcstol(str_fps,&c,10); //get the first number
            CString tempo2 = __targv[3];
            CString full = title + space + tempo1 + space + tempo2;
            Gl_ip = tempo1;
            SetWindowText(full);
      }

The goal is to have a const char*  ("11.1.1.111") for Gl_ip CString variables but the contains is not correct and casting is not good because CString don't like it.

The problem is that  tempo1contain is "1".
What is the problem ? __targv[] is for tchar only ? must I use __wargv[] ?

Thank you
Avatar of pgnatyuk
pgnatyuk
Flag of Israel image

Are you sure that you need to use __targv[1] and not __targv?

Avatar of Develprog
Develprog

ASKER



It seems normal because I'm using it for the first parameter isn't it ?

Thank you

 
How the main function is declared?

int _tmain(int argc, _TCHAR* argv[])
{
   return 0;
}
I attached a code that works

// unicode_par.cpp : Defines the entry point for the console application.
//
#include <tchar.h>
#include <stdio.h>

int _tmain(int argc, _TCHAR* argv[])
{
	if (argc > 1 && argv[1][0] != '\0')
	{
		_tprintf(argv[1]);
	}
	return 0;
}

Open in new window

Avatar of AndyAinscow
here problem tempo1 contain is "1" isntead of "11.1.1.111"

How do you check what tempo1 contains?
Try using AfxMessageBox(tempo1);

One sees a number of questions like this - why doesn't this code work - but it does work in reality, the problem is with the checking during development.
I'd also do a AfxMessageBox( __targv[1]) as well to confirm that is what you want it to be.
Hi,

When using this :
      CString tempo1 = __targv;
I have error :
error C2440: 'initializing' : cannot convert from 'unsigned short ** ' to 'class CString'


But sorry I do not explain clearly I can display the "11.1.1.111" coorectly in a MessageBox
so th e Gl_ip variable is good affected but the problem is more far when using this variable like a parameter for a function :

bool      store=cp.Init(Gl_ip);

that gives error:  C2664: 'Init' : cannot convert parameter 1 from 'class CString' to 'const char *'

So I tried with those codes :

1)
bool store=cp.Init((const char*)Gl_ip.GetBuffer(0));

2)
      char* BufPtr=  NULL;
        WideCharToMultiByte(CP_ACP,WC_DEFAULTCHAR,(LPCTSTR)Gl_ip,-1,BufPtr,Gl_ip.GetLength(),NULL,NULL);
      store=cp.Init(BufPtr);

The both codes compile well but the Init function fails


But when Compiler settings are _MBCS this same code :

bool      store=cp.Init(Gl_ip);

can compile well and the Init function succeed.

So the problem is in Init function when system is unicode, I think we must convert CString  to const char* without any truncation isn'it it ?


Thank you

Try that:
CString tempo1 = __targv[1];

There is a problem with your project settings - it looks like it uses Unicode character set, but your function receives char* as a parameter. So I'm not sure I fully understand how it works and what it is supposed to do. I can guess only.

If your project is in Unicode so CString object is a Unicode text too. If you need to convert it to char* you need to use WideCharToMultiByte. For example,
CString tempo1;
tempo1 = __targv[1];
//..code
char sz[MAX_PATH] = { 0 };
WideCharToMultiByte(...)
cp.Init(sz);
 


I tried too with this :

            CStringA charstr(Gl_ip);
            store=cp.Init(charstr);

and error is :error C2065: 'CStringA' : undeclared identifier


Thank you
OK, _targv[1] is what you think it is.

Try using AfxMessageBox(tempo1);  (from the code you posted originally).


@pgnatyuk - console app, main function ?
Yes :)

the Init function succed with this:

BYTE *p;
p = (BYTE*)malloc(Gl_ip.GetLength());
USES_CONVERSION;
strcpy((char*)p,W2A(Gl_ip));
store=cp.Init((const char*)p);


But is it the good way, can it be possible to have memory corruption or memleak with this ?

Thank you
ASKER CERTIFIED SOLUTION
Avatar of pgnatyuk
pgnatyuk
Flag of Israel image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ok thank you.

But when I use my code is ok but if I add the free like below:

BYTE *p;
p = (BYTE*)malloc(Gl_ip.GetLength());
USES_CONVERSION;
strcpy((char*)p,W2A(Gl_ip));
store=cp.Init((const char*)p);
free(p); //bug

where must I put the free?

Thank you
with this line I have Debug error:

DAMAGE: after normal block (#245)

Thank you

p = (BYTE*)malloc(Gl_ip.GetLength());

This line is wrong. You need to allocate more memory. At least, you need /0 in the end. Why you need BYTE*?

You can declare just char p[MAX_PATH] = { 0 }; and it will work.
If you think the string is longer:

p = (BYTE*)malloc(Gl_ip.GetLength() + 2);
memset(p, 0, Gl_ip.GetLength() + 2);

 


Ok,

It is ok with the wcstombs() with char better than using BYTE and W2A( other unicode function.

Just for know when using WIN32 unicode functions like SendMessageW for example, normally this function must display each unicode string isn't it ? :

SendMessageW(pControl->m_hWnd, WM_SETTEXT, (WPARAM)0, (LPARAM)str);

But I realize that when str is a russian or grec or arabic the control cannot display the string but it show "??????"    ?

Maybe because of my projec is MFC based if so is there MFC function displaying russian string in controls ?  Or must I absolutly  change the Project settings of my projects from _MBCS to _UNICODE  ?
 
The problem is when projetc settings are set to unicode that all my ascii functions must become unicode functions str->wcs.


Thank you

I work with the Unicode projects only. So I write just SendMessage and it is translated to SendMessageW.
To see Russian you need to install Russian language on the PC - the system font should contain the Russian letters. Arabic means right-to-left support. Anyway, the life is easier if my multi-language Windows application uses Unicode character set. In this case, in the MFC application the CString objects contain the Unicode text.

On he General page of the project settings you can set "Unicode Character Set".

How I remember, in the preprocessor definition should be UNICODE and _UNICODE for the Unicode project.
Unicode is a default in VS, so I just don't change it. And everywhere I use just WCHAR or LPWSTR or LPCWSTR and so wcscat instead of strcat and so on.

Take a look here:
http://www.mihai-nita.net/article.php?artID=20060723a

It is not about VS 2008, but it explains something.

Hi,

>>To see Russian you need to install Russian language on the PC - the system font should contain the >>Russian letters. Arabic means right-to-left support. Anyway, the life is easier if my multi-lan

that means maybe that I must set my project settings in VC6 to UNICODE instead of MBCS for ensure that dialogs controls can display all unicode characters ? is 'it really the case  ?

Thank you
On Windows I always prefer UNICODE.
In your case, when the project is ready, it may cause a lot of problems and bugs.