incompatible types pwidechar and string

So I am trying to use variables with ShellExecute but always get this error when compiling [DCC Error] Unit1.pas(44): E2010 Incompatible types: ‘string’ and ‘PWideChar’

I only have 2 variables and they are both strings ssid and pass The line where the error is: ShellExecute(0, nil, ‘cmd.exe’, ‘/c netsh wlan set hostednetwork ss + pass, nil, HIDE_WINDOW);

If you have noticed by now I am trying to make a program that uses cmd to set, stop and start a hotspot. If I use this line ShellExecute(0, nil, ‘cmd.exe’, ‘/c netsh wlan set hostednetwork ss > it works just fine.

Any help? Thx. and yes i am new at delphi so sorry if what i am trying to do wont work

I try to compress files using winrar command line, But when I add a variable in the command line I get these error Incompatible types ‘PWideChar’ and ‘string’ !

I convert the sdate variable to WideChar but it’s not work !!

How I can fix it !

1 Answer 1

The text arguments of ShellExecute are of type PChar . But you supply a string for argument number 4.

The error message is very clear. You know by inspecting the declaration of ShellExecute that the problematic argument is of type PChar (an alias to PWideChar ). And the error message tells you that you are passing a string .

Используется RAD Studio XE. В интернетах удалось обнаружить по этой теме следущее.

var
wideChars : array[0..11] of WideChar;
myString : String;

begin
// Задание значения нашей строке
myString := «Hello World»;

// Копирование в формат WideChar в наш массив
StringToWideChar(myString, wideChars, 12);

// Показываем, что копирование дало
ShowMessage(WideCharToString(wideChars));
end;

function StringToPWide(sStr: string; var iNewSize: integer): PWideChar;
var
pw: PWideChar;
iSize: integer;
begin
iSize := Length(sStr) + 1;
iNewSize := iSize * 2;

MultiByteToWideChar(CP_ACP, 0, PChar(sStr), iSize, pw, iNewSize);

Но ни первое ни второе не работает, а вылетает с ошибками. В первом случае ему не нравится, что ему вместо PWideChar передают массив, если заменить массив на PwideChar, то будет вылет по ошибке связанной с памятью в которую неправильно обратились. Во втором случае он начинает матюкаться на это PChar(sStr).

Вопрос, как же все такие его преобразовать? API функция требует PWideChar.


Писатель ( 2012-09-23 14:31 ) [1]

Забавно вот так 50); работает


Писатель ( 2012-09-23 14:31 ) [2]


Hole ( 2012-09-23 16:26 ) [3]

var
S: AnsiString;
W: WideString;
P: PWideChar;
begin
S := «Hello, World!»;
W := S;
P := PWideChar(W);
end;

Delphi сама выполняет преобразование между типами данных AnsiString, WideString и UnicodeString. Поытому можно вообще написать вот так: PWideChar(WideString(S)) ; но это будет временная переменная, а поэтому пользоваться этим можно только «по месту».

И по поводу того, что вы тут в трех постах написали:

function StringToWideChar(const Source: UnicodeString; Dest: PWideChar; DestSize: Integer): PWideChar;
Returns a UNICODE string from an AnsiString.
StringToWideChar converts a string from ANSI to UNICODE and stores the result in a specified buffer.
Source is the string to convert. It must include at most DestSize – 1 characters.
Dest is the buffer into which StringToWideChar writes the corresponding UNICODE string. The caller allocates this memory to include at least DestSize wide characters. Following the call, Dest contains at most DestSize — 1 characters, terminated by a NULL wide character.
DestSize is the size of the buffer Dest.
StringToWideChar returns a pointer to Dest.

Посмотрите, чему равно SizeOf(WideChar) , и сделайте соотвествующие исправления в коде.

Оцените статью