[英]How to employ DPROJ <ProjectGUID> within Delphi code?
I want to use a GUID to uniquely identify my Application and to get at this value from within the code. 我想使用GUID唯一地标识我的应用程序,并从代码中获取此值。 I see that there is a GUID that would be ideal in the DPROJ: 我看到在DPROJ中有一个理想的GUID:
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ProjectGuid>{D4DB842C-FB4C-481B-8952-77DA04E37102}</ProjectGuid>
Does this get into the exe anywhere, eg as a resource? 这是否可以作为资源进入到exe文件中? If not, what is the neatest way of linking in this GUID value into my exe file and reading it in code. 如果不是,将这个GUID值链接到我的exe文件并用代码读取它的最简洁的方法是什么。 The above GUID resides in a dedicated text file and is pasted into the DPROJ with my DprojMaker tool , so I can INCLUDE it in anything you might suggest. 上面的GUID驻留在专用的文本文件中,并使用我的DprojMaker工具粘贴到DPROJ中 ,因此我可以将其包含在您可能会建议的任何内容中。 Thanks 谢谢
AFAIK the <ProjectGUID>
is not embedded in the Exe file, but you can create an application to read the project guid and insert as a resource in your exe. AFAIK <ProjectGUID>
未嵌入在Exe文件中,但是您可以创建一个应用程序来读取项目guid并作为资源插入exe。
Check this sample app which read a file a create/updates a resource in a exe. 检查此示例应用程序,该应用程序读取文件并在exe中创建/更新资源。
program UpdateResEXE;
{$APPTYPE CONSOLE}
uses
Classes,
Windows,
SysUtils;
//you can improve this method to read the ProjectGUID value directly from the dproj file using XML.
procedure UpdateExeResource(Const Source, ResourceName, ExeFile:string);
var
LStream : TFileStream;
hUpdate : THANDLE;
lpData : Pointer;
cbData : DWORD;
begin
LStream := TFileStream.Create(Source,fmOpenRead or fmShareDenyNone);
try
LStream.Seek(0, soFromBeginning);
cbData:=LStream.Size;
if cbData>0 then
begin
GetMem(lpData,cbData);
try
LStream.Read(lpData^, cbData);
hUpdate:= BeginUpdateResource(PChar(ExeFile), False);
if hUpdate <> 0 then
if UpdateResource(hUpdate, RT_RCDATA, PChar(ResourceName),0,lpData,cbData) then
begin
if not EndUpdateResource(hUpdate,FALSE) then RaiseLastOSError
end
else
RaiseLastOSError
else
RaiseLastOSError;
finally
FreeMem(lpData);
end;
end;
finally
LStream.Free;
end;
end;
begin
try
if ParamCount<>3 then
begin
Writeln('Wrong parameters number');
Halt(1);
end;
Writeln(Format('Adding/Updating resource %s in %s',[ParamStr(2), ParamStr(3)]));
UpdateExeResource( ParamStr(1), ParamStr(2), ParamStr(3));
Writeln('Done');
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.
Now from your app, you can use the Post build events to call this application on this way 现在,从您的应用程序中,您可以使用Post build事件以这种方式调用此应用程序
"C:\The path where is the tool goes here\UpdateResEXE.exe" "C:\The path of the file which contains the ProjectGUID goes here\Foo.txt" Project_GUID "$(OUTPUTPATH)"
And use like so : 像这样使用:
{$APPTYPE CONSOLE}
uses
Windows,
Classes,
System.SysUtils;
function GetProjectGUID : string;
var
RS: TResourceStream;
SS: TStringStream;
begin
RS := TResourceStream.Create(HInstance, 'Project_GUID', RT_RCDATA);
try
SS:=TStringStream.Create;
try
SS.CopyFrom(RS, RS.Size);
Result:= SS.DataString;
finally
SS.Free;
end;
finally
RS.Free;
end;
end;
begin
try
Writeln(Format('Project GUID %s',[GetProjectGUID]));
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
readln;
end.
Why not just hard-code your own GUID inside your code itself? 为什么不只是在代码本身内部硬编码自己的GUID? The Code Editor has a CTRL+SHIFT+G
keyboard shortcut for generating a new GUID string at the current active line of code. 代码编辑器具有CTRL+SHIFT+G
键盘快捷键,用于在当前活动代码行生成新的GUID字符串。 You can tweak that declaration into a constant variable for your code to use as needed, eg: 您可以将该声明调整为常量,以供您的代码根据需要使用,例如:
const
MyGuid: TGUID = '{04573E0E-DE08-4796-A5BB-E5F1F17D51F7}';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.