0
std::string sszModName = "kernel32.dll";
std::string WinVersion = "WIN81";
std::string MachineGUID= "ce9e95db-5fda-436a-b29a-f5537702c77d";

char buf[1024];
sprintf(buf, "https://nulln.nullnu-ll.nul/nullnulln/api/ireport.php?module=%s&publisher=%s&win=%s&machineguid=%s", sszModName, "ERROR_HASH_VERIFY", WinVersion, MachineGUID);

This code causes program lag, could you help me figure out why?

memm
  • 13
  • 3
  • 2
    `sprintf` is a C function. It doesn't know anything about C++ objects. A decent compiler should shout warnings at you for attempting to use C++ `std::string` objects as arguments, when the `%s` format expects a `char *`. – Some programmer dude Aug 04 '22 at 08:45
  • And I really advice against `sprintf` (or any `printf`-like function) as they are not type-safe. For strings, you can concatenate strings using the plain `+` operator. Like `https://nulln.nullnu-ll.nul/nullnulln/api/ireport.php?module=" + sszModName + "..."` etc. The result is another `std::string`. If you have other non-string data, use an output string stream and use it just like you use `std::cout` to construct the string. Another alternative is [`std::format`](https://en.cppreference.com/w/cpp/utility/format/format), but it's not commonly supported everywhere yet. – Some programmer dude Aug 04 '22 at 08:46
  • Note that `sszModName` is not a good name for a `std::string` variable. The prefix `ssz` is used throughout the WinAPI and denotes a NULL-terminated C string. Besides that, it's inconsistent with the naming of your other variables. – paddy Aug 04 '22 at 08:48
  • On a different note, you seem to have misunderstood some of the terms involved. "Lag" is when a program takes longer to perform a task than expected, or when network communications seem to slow down. A "crash" is when a program unexpectedly exits with an exception or similar. And a build error or warning is when the compiler and linker tells you that something in the code is wrong. – Some programmer dude Aug 08 '22 at 06:55

1 Answers1

1

Try

sprintf(buf, 
    "https://nulln.nullnu-ll.nul/nullnulln/api/ireport.php?module=%s&publisher=%s&win=%s&machineguid=%s", 
    sszModName.c_str(), 
    "ERROR_HASH_VERIFY", 
    WinVersion.c_str(), 
    MachineGUID.c_str());

C strings are not the same as C++ strings. spprintf only uses C strings so you must use .c_str() to turn your C++ strings into C strings.

john
  • 85,011
  • 4
  • 57
  • 81