I have a function in a DLL which returns PChar as a result. I debugged the DLL and verified it in fact does assign the Result. I even tested hard-coding the result. However, when the DLL function is returned back to the app, the Result is unassigned. Other functions made the exact same only returning Bool work perfect, just returning PChar has a problem...
DLL Code:
App Code:
In the end, when I try to pass the result PChar to a String, I get an access violation.
JD Solutions
DLL Code:
Code:
function svc_GetServiceName(Filename: PChar): PChar; StdCall;
var
Reg: TRegistry;
L: TStringList;
Key, N, NKey, Res: String;
X: Integer;
Found: Bool;
begin
Found:= False;
Result:= PChar('');
Reg:= TRegistry.Create(KEY_READ or KEY_WRITE);
L:= TStringList.Create;
try
Reg.RootKey:= HKEY_LOCAL_MACHINE;
Key:= 'System\CurrentControlSet\Services\';
Reg.OpenKey(Key, False);
Reg.GetKeyNames(L);
Reg.CloseKey;
for X:= 0 to L.Count - 1 do begin
N:= L[X];
NKey:= Key + N;
if Reg.OpenKey(NKey, False) then begin
if Reg.ValueExists('ImagePath') then begin
if UpperCase(Filename) = UpperCase(Reg.ReadString('ImagePath')) then begin
//ShowMessage('Found!');
Res:= N;
Found:= True;
end;
end;
Reg.CloseKey;
end;
if Found then Break;
end;
finally
Reg.Free;
L.Free;
Result:= PChar(Res);
end;
end;
exports
svc_GetServiceName;
App Code:
Code:
var
SvcName: PChar;
function svc_GetServiceName(Filename: PChar): PChar; StdCall;
External 'WinSvc32Lib.dll';
/////////////////////////////////////////////////////////////////////////////
SvcName:= PChar(svc_GetServiceName(PChar(dlgOpen.FileName)));
In the end, when I try to pass the result PChar to a String, I get an access violation.
JD Solutions