Showing posts with label MFC. Show all posts
Showing posts with label MFC. Show all posts

5/30/2018

How to get current directory in window application.

Example source code to get current directory in window application


..
CString CurrentDirectoryToCString()
{
    TCHAR buff[MAX_PATH];
    memset(buff, 0, MAX_PATH);
    ::GetCurrentDirectory(MAX_PATH, buff);
    CString strFolder = buff;

    return strFolder;
}
..

6/29/2017

Error MSB8031, multi byte character set problem in vs 2013

error MSB8031: Building an MFC project for a non-Unicode character set is deprecated. You must change the project property to Unicode or download an additional library. See http://go.microsoft.com/fwlink/p/?LinkId=286820 for more information.

Don't worry about that.
VS 2013 is not support MFC library for MBCS.
They recommend unicode default.

So it can solve easily.
Go below url, download and install.

6/19/2017

tip, CString token in MFC

refer to below code.


CString selectedModel;
m_ListBoxOfConnection.GetText(sel, selectedModel);
//selectedModel = "com1,model1,base";

CString comStr;
CString modelStr;
CString optionStr;

AfxExtractSubString(comStr, selectedModel, 0, ',');
AfxExtractSubString(modelStr, selectedModel, 1, ',');
AfxExtractSubString(optionStr, selectedModel, 2, ',');


//then
//comStr = com1
//modelStr = model1
//optionStr = base


Thank you.



6/18/2017

small tip, mfc listbox all delete

ListBox.ResetContent();

^^

tip, CString to int, MFC

CString str; 
int integer; 

integer = _wtoi(str); // wide charater formats 
integer = _atoi(str); // otherwise


Thank you~!!


1/03/2017

In MFC, File exist check and delete the file, example source code.



If file exist then delete the file, example source code in MFC

< github code >
___

MFC Encode / Decode example souce code



WtoC function code is here
http://study.marearts.com/2017/01/unicode-cstring-convert-to-char.html

And refer to GetMacAddress code
http://study.marearts.com/2017/01/get-mac-address-in-mfc.html

Encode code
//

..

Decode code
//

..

Unicode CString convert to char *

The returned char * will need to be freed after use.


< gist code start >

< gist code end >


#tags
wchar_t, WtoC, WideCharToMultiByte, WideCharToMultiByte

useful site for CString converting


http://www.flounder.com/cstring.htm#Converting%20a%20CString%20to%20an%20integer


Get Mac Address in MFC


GetMacAddress in MFC

Get by using GetAdaptersInfo function
//

..
Get by using ip address
//If localhost -> GetMacAddress(_T("*"));
//if have ip -> GetMacAddress(_T("192.168.1.1"));//

..


12/08/2016

convert char* to LPWSTR

refer to below example


char text[] = "something";
wchar_t wtext[20];
mbstowcs(wtext, text, strlen(text)+1);//Plus null
LPWSTR ptr = wtext;

12/07/2016

convert tchar * to char * / char * to tchar*

tchar* -> char*

"Tch" is tchar*
"pStrTch" is char*


TCHAR Tch[MAX_PATH + 1];
int lenOftch = (wcslen(Tch) + 1) * 2;
char * pStrTch = (char*)malloc(sizeof(char)*MAX_PATH);

//tchar* to char*
WideCharToMultiByte(CP_UTF8, 0, Tch, -1, pStrTch, lenOftch, NULL, NULL);
//


char* -> tchar*


char charBuff[]="test";
TCHAR szUniCode[256]={0,};
MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, charBuff, strlen(charBuff), szUniCode, 256);

12/01/2016

Create a console window in MFC

Insert code in stdafx.h

#pragma comment(linker, "/entry:WinMainCRTStartup /subsystem:console")

The print result is output.



string to LPCTSTR

Very simple way~


string s;
CString cs(s.c_str()); //< - lpctstr


11/30/2016

Set up VisualStudio to run programs as an administrator



Enter the project properties and change the Configuration Properties -> Linker -> Manifest File -> UAC Execution Level item to requireAdministrator.


9/23/2016

To run an external file to ShellExecute and kill processes. in MFC (and window active by PID or process handle)




Firstly, Check executing process to make by me.
And If process exist made by me, kill after check that saved PID is same.
"TerminateProcess" function used for kill process.
...
...
if (m_Manual_shell_hadle != NULL)
 {
  unsigned int tmp_pid = GetProcessId(m_Manual_shell_hadle); // retrieve PID
  //It comes under the pid value to the storage handle. Take on only kill pid is the same as the saved pid.
  if (tmp_pid == m_Manual_shell_pid)
  {
   TerminateProcess(m_Manual_shell_hadle, 1); //kill process
   m_Manual_shell_hadle = NULL;

   return 1; //sucess
  }
  
 }
 m_Manual_shell_hadle = NULL;
return 0; //not found
...
...


And, execute external file.

..
...
 SHELLEXECUTEINFO ShExecInfo = { 0 };
 ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
 ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
 ShExecInfo.hwnd = this->m_hWnd;
 ShExecInfo.lpVerb = NULL;
        ShExecInfo.lpFile = TargetPath; //external file name
 ShExecInfo.lpParameters = NULL; //aguments
 ShExecInfo.lpDirectory = NULL;
 ShExecInfo.nShow = SW_SHOW;
 ShExecInfo.hInstApp = NULL;

ShellExecuteEx(&ShExecInfo); //execute 

  //save pid and handle
  m_Manual_shell_pid = GetProcessId(ShExecInfo.hProcess); // retrieve PID
  m_Manual_shell_hadle = ShExecInfo.hProcess;

...
...


"m_Manual_shell_pid, m_Manual_shell_hadle" is our member variable.


and one more thing, if we want to window activating or taking to top on screen, using hwnd, to use below function.
///
::BringWindowToTop(hwnd);
 ::SetActiveWindow(hwnd);
 ::SetForegroundWindow(hwnd);
 ::ShowWindow(hwnd, SW_SHOWNORMAL);
 ::UpdateWindow(hwnd);
///

But to use these functions, we have to know hwnd value to active window.
We just know HANDLE value, then we can get Process id, that PID using GetProcessId and then
we can know hwnd using by this pid.

this is little complex, we should use callback function this -> EnumWindows(...)
The usage is like that,

firstly, make own function -> EnumWindowsProcMy
And call EnumWindows and then get hwnd.
see example code

HWND g_HWND = NULL;
BOOL CALLBACK EnumWindowsProcMy(HWND hwnd, LPARAM lParam)
{
 DWORD lpdwProcessId;
 GetWindowThreadProcessId(hwnd, &lpdwProcessId);
 if (lpdwProcessId == lParam)
 {
  g_HWND = hwnd;
  return FALSE;
 }
 return TRUE;
}


int CSomeClass::ActiveManualWindowIfAlive()
{

        if (m_Manual_shell_hadle != NULL)
 {
  unsigned int tmp_pid = GetProcessId(m_Manual_shell_hadle); // retrieve PID
  if (tmp_pid == m_Manual_shell_pid)
  {
   g_HWND = NULL;
   EnumWindows(EnumWindowsProcMy, m_Manual_shell_pid);
   if (g_HWND != NULL)
   {
    ::BringWindowToTop(g_HWND);
    return 1;
   }

  }
  
 }

 return 0;

}



Thank you.