Love My Love

Program C Basic Tips [C/C++]

2019.08.24

Continuous update!

Misc

  • VC6.0 non-console program output debugging method:
    • download DebugView and run, insert code below to you program:
OutputDebugString(charParam);
OutputDebugStringW(wchar_tParam);
i = 1;
char stringMerged[100] = { 0 };
sprintf(stringMerged, "%s%d", filename, i);  // stringMerged == "a string ", i == 123
// now, stringMerged == "a string 123"
  • ‘wcslen’ : cannot convert parameter 1 from ‘char [260]’ to ‘const unsigned short *’

  • get argv[1]:

char* filename = new char[argv[1].length() + 1];
strcpy(filename, keyword.c_str());
// now, filename is the first arg's value
  • get arguments from WinMain()’s lpCmdLine:
    • method one:
#include <stdlib.h>
...
    int argc = __argc;;
    char** argv = __argv;
...
+ mothod two: [CommandLineToArgvW function](https://docs.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-commandlinetoargvw)
  • Example for the using above:
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>  // for sprintf
#include <stdlib.h>

//  Forward declarations:
int main(int argc, char* argv[]);

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
    int argc = __argc;
    char** argv = __argv;
    main(argc, argv);
    return 0;
}

int main(int argc, char* argv[])
{
    char args[100] = "appName.exe";
    if(argc == 2)
    {
        sprintf(args, "%s %s", args, argv[1]);
    }
    if(argc == 3)
    {
        sprintf(args, "%s %s %s", args, argv[1], argv[2]);
    }
    WinExec(args,SW_HIDE);
    return 0;
}

Useful functions

  • Create Dir:
#define R_OK 4 /* Test for read permission. */
#define W_OK 2 /* Test for write permission. */
#define X_OK 1 /* Test for execute permission. */
#define F_OK 0 /* Test for existence. */
int DirCreate(string path)
{
    // if _access(path.c_str(), R_OK) == 0 the folder exist!
    if (_access(path.c_str(), R_OK) == -1)  // vc6.0
    {
        int flag = _mkdir(path.c_str()); // mkdir successfully return 0
        return flag;
    }
    return 0;
}
  • Get Tmp folder:
char tmp[200];
int GetTmp()
{
    DWORD dwRetVal = 0;
    TCHAR szTempFileName[MAX_PATH];
    TCHAR lpTempPathBuffer[MAX_PATH];
    HANDLE hFile = INVALID_HANDLE_VALUE;
    dwRetVal = GetTempPath(MAX_PATH, lpTempPathBuffer);
    if (dwRetVal < MAX_PATH || (dwRetVal != 0))
    {
        memset(tmp, 0, 200);
        wcstombs(tmp, lpTempPathBuffer, wcslen(lpTempPathBuffer) + 1);
    }
    return 0;
}

Convert types

  • const char* to char*:
strdup("My string literal!");  // method one
// or
(char*)"My string literal!"  // method two
  • char* argv[] to int:
char *p;
capNum = strtol(argv[2], &p, 10);
  • char [] to wchar_t*
char filepath[100] = { 0 };
filepath = "filename";
wchar_t* wfilepath = new wchar_t[50];
swprintf(wfilepath, L"%hs", filepath);  // vc6.0
swprintf(wfilepath, 50, L"%hs", filepath);  // vs2015 vs2019
  • char* to string:
char *buf = "test";
string s = buf;
// or
char *buf = "test";
std::string s(buf);
// or 
char *buf = "test";
std::string s(buf, sizeof(buf));
发表评论