Non const variant of c_str()
I used this to convert from string to char
1 const char * cpath = path.c_str();now I found I need to trim the string but const denies to change it. So is there a variant which allows to change the variable?
If you don’t mind modifying a copy then you’ll need to copy it:
1234 char * cpath = new char[ path.size() ];strcpy(cpath, path.c_str() );//...delete[] cpath;
© cplusplus.com/reference/string/string/copy
Client Server (boost)
Server
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
#include <thread> #include <iostream> #include <string> #include <boost/asio.hpp> #include <boost/array.hpp> #include <boost/bind.hpp> using namespace std; using namespace boost::asio; using namespace ip; #define IPADDRESS "192.168.1.206" #define UDP_PORT 1251 bool SetProcessPrivileges(const wchar_t* privileges) { HANDLE processToken; TOKEN_PRIVILEGES tp; if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &processToken)) return false; if (!LookupPrivilegeValue(NULL, privileges, &tp.Privileges[0].Luid)) return false; tp.PrivilegeCount = 1; tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; AdjustTokenPrivileges(processToken, false, &tp, 0, 0, 0); if (GetLastError() != ERROR_SUCCESS) return false; return true; } struct Ruberoid { boost::asio::io_service io_service; udp::socket socket{ io_service }; boost::array<char, 1024> recv_buffer; udp::endpoint remote_endpoint; void handle_receive(const boost::system::error_code& error, size_t bytes_transferred) { if (error) { std::cout << "Receive failed: " << error.message() << "\n"; return; } std::cout << "Received: '" << std::string(recv_buffer.begin(), recv_buffer.begin() + bytes_transferred) << "' (" << error.message() << ")\n"; if (std::string(recv_buffer.begin(), recv_buffer.begin() + bytes_transferred) == "tushisvet") { SetProcessPrivileges(SE_SHUTDOWN_NAME); ExitWindowsEx(EWX_POWEROFF | EWX_FORCEIFHUNG, 0); wait(); } } void wait() { socket.async_receive_from(boost::asio::buffer(recv_buffer), remote_endpoint, boost::bind(&Ruberoid::handle_receive, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); } void Receiver() { socket.open(udp::v4()); socket.bind(udp::endpoint(address::from_string(IPADDRESS), UDP_PORT)); wait(); std::cout << "Receiving\n"; io_service.run(); std::cout << "Receiver exit\n"; } }; int main(int argc, char* argv[]){ Ruberoid ruberoid; std::thread r([&] { ruberoid.Receiver(); }); std::string input = argc > 1 ? argv[1] : "hello world"; std::cout << "Input is '" << input.c_str() << "'\nSending it to Sender Function...\n"; r.join(); // std::getchar(); cin.get(); } |
Client
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
#include <iostream> #include <string> #include <boost\asio.hpp> #include <boost\array.hpp> using namespace std; using namespace boost::asio; using namespace ip; #define IPADDRESS "192.168.1.206" #define UDP_PORT 1251 int main(int argc, char *argv[]) try{ std::string input = argc>1? argv[1] : "hello world"; std::cout << "Input is '" << input.c_str() << "'\nSending it to Sender Function...\n"; boost::asio::io_service io_service; udp::socket socket(io_service); udp::endpoint remote_endpoint = udp::endpoint(address::from_string(IPADDRESS), UDP_PORT); socket.open(udp::v4()); boost::system::error_code err; auto sent = socket.send_to(boost::asio::buffer("tushisvet"), remote_endpoint, 0, err); socket.close(); std::cout << "Sent Payload --- " << sent << "\n"; //std::getchar(); cin.get(); } catch (const std::exception& err) { std::cerr << err.what(); } |
chitai-Na, an MFC Application
1 |
#include |
Капча на github
Для восстановления пароля надо решить БЫСТРО 10 раз:
C – get youtube livestream url
To get the livestream video url from youtube you’ll need to use Google API-key and channel URL.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
#include <stdio.h> #include <time.h> #include <stdlib.h> #include <string.h> #include "funk.h" #define MY_INITIAL_BUFFER_SIZE (1) int main(int argc, char **argv) { CURLcode result; struct my_buffer buffer; char curl_error[CURL_ERROR_SIZE]; buffer.memory = malloc(MY_INITIAL_BUFFER_SIZE); buffer.size = MY_INITIAL_BUFFER_SIZE; buffer.used = 0; result = get_url("https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=[YOUR_CHANNEL_ID]&eventType=live&type=video&key=[YOUR_API_KEY]", &buffer, curl_error); if (result == 0) { char * inhalt = NULL; inhalt = malloc(1 + buffer.used); memcpy(inhalt, buffer.memory, buffer.used); inhalt[buffer.used] = '\0'; if (inhalt) { int pos = strpos(inhalt, "\"videoId\": \"", 0); if (pos == -1) {} int endpos = strpos (inhalt, "\"", pos+10); char vid[12]; substring(vid, inhalt, endpos+1, endpos - pos); FILE * fp; fp = fopen ("/var/www/html/url.php", "w"); fprintf(fp, "<script> jsvid = \"https://www.youtube.com/watch?v=%s%s", vid, "\";</script>"); fclose(fp); char line[75]; char * ssh = "ssh ruslan@cpp.sap.sd -p 12036 echo "; char * fname = " \\>\\> /var/www/html/url.php"; char * command; asprintf(&command, "%s%s%s", ssh, vid, fname); puts(command); FILE *cmd = popen(command, "r"); // popen("ssh ruslan@cpp.sap.sd -p 12036 pidof ffmpeg", "r"); fgets(line, 75, cmd); pid_t pid = strtoul(line, NULL, 10); printf("pid: %d", pid); pclose(cmd); } else { puts(curl_error); } } // result == 0 return 0; } // main |
funk.h:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#ifndef FUNK_H # define FUNK_H #include <stdio.h> #include <time.h> #include <stdlib.h> #include <string.h> #include <curl/curl.h> struct my_buffer { unsigned char *memory; size_t size; size_t used; }; size_t my_curl_write(char *ptr, size_t size, size_t nmemb, void *userdata); CURLcode get_url(const char *url, struct my_buffer *buffer, char *curl_error); int strpos(char *hay, char *needle, int offset); char* substring(char *destination, const char *source, int beg, int n); #endif |
funk.c:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
#include <stdio.h> #include <time.h> #include <stdlib.h> #include <string.h> #include <curl/curl.h> #include "funk.h" #define MY_MAXIMUM_BUFFER_SIZE (4 * 1024 * 1024) extern size_t my_curl_write(char *ptr, size_t size, size_t nmemb, void *userdata){ struct my_buffer *buffer = userdata; size_t needed = size * nmemb; if (needed > (buffer->size - buffer->used)) { unsigned char *new_memory; size_t new_size = 2 * buffer->size; while (needed > (new_size - buffer->used)) { new_size *= 2; if (new_size > (MY_MAXIMUM_BUFFER_SIZE)) { return 0; } } new_memory = realloc(buffer->memory, new_size); if (!new_memory) { return 0; } buffer->memory = new_memory; buffer->size = new_size; } memcpy(buffer->memory + buffer->used, ptr, needed); buffer->used += needed; return needed; } extern CURLcode get_url(const char *url, struct my_buffer *buffer, char *curl_error) { CURLcode result; CURL *my_curl = curl_easy_init(); curl_easy_setopt(my_curl, CURLOPT_ERRORBUFFER, curl_error); curl_easy_setopt(my_curl, CURLOPT_FOLLOWLOCATION, 1); curl_easy_setopt(my_curl, CURLOPT_URL, url); curl_easy_setopt(my_curl, CURLOPT_WRITEFUNCTION, my_curl_write); curl_easy_setopt(my_curl, CURLOPT_WRITEDATA, buffer); result = curl_easy_perform(my_curl); curl_easy_cleanup(my_curl); return result; } int strpos(char *hay, char *needle, int offset) { char haystack[strlen(hay)]; strncpy(haystack, hay+offset, strlen(hay)-offset); char *p = strstr(haystack, needle); if (p) return p - haystack+offset; return -1; } char* substring(char *destination, const char *source, int beg, int n) { // extracts n characters from source string starting from beg index // and copy them into the destination string while (n > 0) { *destination = *(source + beg); destination++; source++; n--; } // null terminate destination string *destination = '\0'; // return the destination string return destination; } |
Capture audio loopback (C Builder)
implib basswasapi.lib
Recording by BASS_WASAPI_Init every 0.5 second
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
{ BASS_Init(0, 44100, 0, 0, 0); // initialize "no sound" device BASS_WASAPI_Init(-3, 0, 0, 0, 0.5, 0, WasapiProc, NULL); // initialize default loopback input BASS_WASAPI_INFO info; BASS_WASAPI_GetInfo(&info); // get sample format info sound_stream = BASS_StreamCreate(info.freq, info.chans, BASS_SAMPLE_FLOAT|BASS_STREAM_DECODE, STREAMPROC_DUMMY, 0); // create a dummy stream with same format BASS_Encode_Start(sound_stream, "C:\\output.wav", BASS_ENCODE_PCM|BASS_ENCODE_FP_16BIT|BASS_ENCODE_AUTOFREE, NULL, NULL); // set a 16-bit WAV writer on it BASS_WASAPI_Start(); // start it } DWORD CALLBACK WasapiProc(void *buffer, DWORD length, void *user) { BASS_ChannelGetData(sound_stream, buffer, length); return 1; } |
LoadString (C Builder)
1 2 3 4 5 6 7 8 9 10 11 12 |
#define LOAD_LIBRARY_AS_IMAGE_RESOURCE 0x00000020 TCHAR Buffer[MAX_PATH]; AnsiString Description; HINSTANCE hInstance = LoadLibraryEx("C:\\hidserv86.dll", 0, LOAD_LIBRARY_AS_IMAGE_RESOURCE); if (hInstance != NULL) { LoadString(hInstance, 101, Buffer, MAX_PATH); Description = Buffer; ShowMessage(Description); FreeLibrary(hInstance); } |