Type conversion from string to const wchar_t* . Type does not match

Andreas Hauschild

Hi i have the following function which i want to call:

uintptr_t GetModuleBaseAddress(DWORD procId, const wchar_t* modName)

Now i want to write a support function which takes a given string and converts it to the target parameter 'const wchar_t* modName'

I have the folloing function:

wchar_t* stringToWchar(std::string s) {
  std::wstring widestr = std::wstring(s.begin(), s.end());
  const wchar_t* widestr = widestr.c_str();
  return widestr;
}

at the return line i get the error: "no suitable conversion function from to exists".

What do i miss here?

In the final result i want to make a call like:

GetModuleBaseAddress(procId, stringToWchar("module.exe"))

Thx.

john

Rewrite your function to return a wstring

std::wstring stringToWchar(std::string s) {
    return std::wstring(s.begin(), s.end());
}

then use it like this

GetModuleBaseAddress(procId, stringToWchar("module.exe").c_str());

Your code was attempting to return a pointer to an object which no longer exists (widestr). You also declared the variable widestr twice, and you attempted to remove the const qualifier.

Don't program with pointers if you can help it. If you need a pointer for some third party API then generate the pointer at the point you call the third party function, not before.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

String conversion from System::String ^ to const wchar_t *

C++ Argument of type "const wchar_t" * incompatible with parameter of type "wchar_t"

Argument of type "const wchar_t*" is incompatible with parameter of type "LPTSTR"

How to transform const wchar_t type to LPTSTR (C++)

Error: argument of type "const wchar_t *" is incompatible with parameter of type "WCHAR *"

Conversion of wchar_t* to string

How could I assign the value from lua_tostring() to a variable in type of wstring or const wchar_t* in utf-8

g++ wchar_t string litteral is not of expected type

invalid conversion from ‘const type*’ to ‘type*’

Visual Studio 2017; argument of type "const wchar_t *" is incompatible with parameter of type "PWSTR" when /permissive is enabled

C++ wifstream: Incompatible type char const*, wchar_t const*

How to fix " State Error (active) E0513 a value of type “const wchar_t *” cannot be assigned to an entity of type “wchar_t *”"

Generic type conversion FROM string

Conversion from string to generic type

Conversion from type 'DBNull' to type 'String' is not valid

Conversion from type 'Button' to type string is not valid

Understanding wchar_t type in c++

Return value type does not match the function type CONST

String parameter const char* and const wchar_t*

t_tone_array type does not match string literal

4-byte wchar_t to String conversion using JNI

jscodeshift throwing error: does not match type string

custom type conversion from string to some object

Conversion From String To Type 'Date' Is Not Valid

conversion from string to TimeStamp data type

“Conversion from string ” “ to type 'Double' is not valid” VB

Conversion from string to type Double is not valid

Error "conversion from string to type decimal is not Valid"

Conversion from string yyyyMMddHHmmss to type integer is not valid

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive