大大们好,小弟我的问题真多,又要麻烦您们了
![]()
让程式只执行一次,站内有很多的内容:
class="code">
unit Unit2; interface uses Windows, SysUtils, TlHelp32; function MapGlobalData(const MapName: string; Size: Integer; var Ptr: Pointer): THandle;
procedure ReleaseGlobalData(var Handle: THandle; var Ptr: Pointer); function IsGlobalDataExistent(const MapName: string): Boolean; implementation function MapGlobalData(const MapName: string; Size: Integer; var Ptr: Pointer): THandle;
begin
Result := CreateFileMapping($FFFFFFFF, nil, PAGE_READWRITE, 0, Size, PChar(MapName));
if Result = 0 then
if GetLastError = ERROR_ALREADY_EXISTS then
begin
Result := OpenFileMapping(FILE_MAP_ALL_ACCESS, False, PChar(MapName));
if Result = 0 then Exit;
end else Exit;
Ptr := MapViewOfFile(Result, FILE_MAP_ALL_ACCESS, 0, 0, 0);
if Ptr = nil then
begin
CloseHandle(Result);
Result := 0;
end;
end; procedure ReleaseGlobalData(var Handle: THandle; var Ptr: Pointer);
begin
if Assigned(Ptr) then
begin
UnmapViewOfFile(Ptr);
Ptr := nil;
end; if Handle <> 0 then
begin
CloseHandle(Handle);
Handle := 0;
end;
end; function IsGlobalDataExistent(const MapName: string): Boolean;
var
hMap: THandle;
begin
hMap := OpenFileMapping(FILE_MAP_ALL_ACCESS, False, PChar(MapName));
Result := hMap <> 0;
if Result then CloseHandle(hMap);
end;
然后在BCB中把这个单元文件添加到工程中去(Project--->Add to Project),然后把该单元编译一下.在BCB的工程中修改为:
//--------------------------------------------------------------------------- #include
#pragma hdrstop
USERES("Project1.res");
USEFORM("Unit1.cpp", Form1);
USEUNIT("Unit2.pas");
//---------------------------------------------------------------------------
#include "Unit2.hpp"
WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
typedef struct{
HWND AppWnd;
} TSharedStruct, *PSharedStruct; const char* MapName = "TestMap"; PSharedStruct GlobalPointer;
THandle hMap; if(IsGlobalDataExistent(MapName))
{
hMap = MapGlobalData(MapName,sizeof(TSharedStruct),Pointer(GlobalPointer)); if(IsIconic(GlobalPointer->AppWnd))
ShowWindow(GlobalPointer->AppWnd,SW_RESTORE);
else
SetForegroundWindow(GlobalPointer->AppWnd); ReleaseGlobalData(hMap,Pointer(GlobalPointer)); return 0;
} hMap = MapGlobalData(MapName,sizeof(TSharedStruct),Pointer(GlobalPointer));
GlobalPointer->AppWnd = Application->Handle; try
{
Application->Initialize();
Application->CreateForm(__classid(TForm1), &Form1);
Application->Run();
}
catch (Exception &exception)
{
Application->ShowException(&exception);
} ReleaseGlobalData(hMap,Pointer(GlobalPointer));
return 0;
}
//---------------------------------------------------------------------------
用这样的方法很好的实现了只让程序执行一次,而且如果前一个程序副本是最小化时还能将其带到前面来.
但是我在把Unit2.pas转化为BCB单元文件时就出问题了
[code]
//--------------------------------------------------------------------------- #include
#pragma hdrstop #include "Unit3.h" //---------------------------------------------------------------------------
HANDLE __fastcall MapGlobalData(AnsiString MapName,int Size,void * &Ptr)
{
HANDLE hMapFile;
char *szBuf;
lstrcpy(szBuf,MapName.c_str()); hMapFile = CreateFileMapping(0xFFFFFFFF,NULL,PAGE_READWRITE,0,Size,szBuf); //这有错误,我不知道该怎样改 if(hMapFile == 0)
{
if(GetLastError = ERROR_ALREADY_EXISTS)
{
hMapFile = OpenFileMapping(FILE_MAP_ALL_ACCESS,False,szBuf); //同样的这也有错误
if(hMapFile == 0)
{
exit(EXIT_FAILURE);
}
}
else exit(EXIT_FAILURE);
} Ptr = MapViewOfFile(hMapFile,FILE_MAP_ALL_ACCESS,0,0,0);
if(Ptr == NULL)
{
CloseHandle(hMapFile);
hMapFile = 0;
} return hMapFile;
}
void __fastcall ReleaseGlobalData(unsigned &Handle, void * &Ptr)
{
//不知道C 中该怎样判断指针是否为空
>
不看不知道,一编译就有 > 人生在勤,不索何获?