C/C++ Bookmarks

Published: 2019-08-04, Updated: 2020-06-21

Links

Problema de cast

error: invalid conversion from 'LPCVOID' {aka 'const void*'} to 'LPVOID' {aka 'void*'} [-fpermissive]
 VirtualFreeEx(hProcess, codep, 0, MEM_RELEASE);

Use o reinterpret_cast

LPVOID lpFunction = ...;
DWORD_PTR dwpFunction = reinterpret_cast<DWORD_PTR>(lpFunction);

Listar funções de uma library

nm -D /lib/$(uname -m)-linux-gnu/libc-*.so | grep -vw U | grep -v "_" | cut -d " " -f3

Esconder a console do programa

#define _WIN32_WINNT 0x0500
#include <windows.h> 
#include <winuser.h> 
#include <stdio.h> 

int main(){
	HWND hWnd = GetConsoleWindow();
    ShowWindow( hWnd, SW_MINIMIZE );  //won't hide the window without SW_MINIMIZE
    ShowWindow( hWnd, SW_HIDE );
}

Ler string com espacos

Use:

fgets (name, 100, stdin);

100 is the max length of the buffer. You should adjust it as per your need.

Use:

scanf ("%[^\n]%*c", name);

Imprimir char no c

printf("%c", 'h');

Comparar duas strings

#include <stdio.h>
#include <string.h>

int main(int argc, char **argv)
{
    int v;
    const char *str1 = "hello";
    const char *str2 = "world";

    v = strcmp(str1, str2);

    if (v < 0)
        printf("'%s' is less than '%s'.\n", str1, str2);
    else if (v == 0)
        printf("'%s' equals '%s'.\n", str1, str2);
    else if (v > 0)
        printf("'%s' is greater than '%s'.\n", str1, str2);

    return 0;
}

Tabela de formatos do printf

Specifier Output Example
d or i Signed decimal integer 392
u Unsigned decimal integer 7235
o Unsigned octal 610
x Unsigned hexadecimal integer 7fa
X Unsigned hexadecimal integer (uppercase) 7FA
f Decimal floating point, lowercase 392.65
F Decimal floating point, uppercase 392.65
e Scientific notation (mantissa/exponent), lowercase 3.9265e+2
E Scientific notation (mantissa/exponent), uppercase 3.9265E+2
g Use the shortest representation: %e or %f 392.65
G Use the shortest representation: %E or %F 392.65
a Hexadecimal floating point, lowercase -0xc.90fep-2
A Hexadecimal floating point, uppercase -0XC.90FEP-2
c Character a
s String of characters sample
p Pointer address b8000000
n Nothing printed. The corresponding argument must be a pointer to a signed int. The number of characters written so far is stored in the pointed location.
% A % followed by another % character will write a single % to the stream. %

Carregando bibliotecas dinamicas no linux

Nao testado

#include <stdlib.h>
#include <stdio.h>
#include <dlfcn.h>

int main(int argc, char **argv) {
	
    void *handle;
    int (*cosine)(const char * format, ...);
    char *error;

    handle = dlopen ("c", RTLD_LAZY);
    if (!handle) {
        fputs (dlerror(), stderr);
        exit(1);
    }

    cosine = dlsym(handle, "cos");
    if ((error = dlerror()) != NULL)  {
        fputs(error, stderr);
        exit(1);
    }

    (*cosine)("%s", "Hello World!!!");
    dlclose(handle);
}

Compile shared library in g++

main.cpp

#include <string.h>
#include <iostream>
#include <map>
#include <utility>

using namespace std;

extern "C" map<int, string>  findUserById();

map<int, string> findUserById(){
	map<int, string> user;
	user[1] = "Bob";
	user[2] = "Mark";
	user[3] = "Ana";
	return user;
}

compiling

g++ -Wl,-z,defs -fPIC -shared -o map.so map.cpp 

checking the symbols

$ objdump -t map.so  | grep "find"
0000000000003a75 g     F .text	00000000000000bb              findUserById

Iterate over map

map<int, int>::iterator it;
for ( it = players.begin(); it != players.end(); it++ ) {
    std::cout << it->first  // string (key)
              << ':'
              << it->second   // string's value 
              << std::endl ;
}

keywords

cpp commands, c++ commands, c commands


Blender Commands Aprendendo Bitwise (Operadores bit a bit ) de uma vez por todas

Comments