Reference: Memory Objects (external site)
At present, this specification depends heavily on Windows-specific behavior and is not intended for implementation on other operating systems.
Shared memory and Mutexes exist on other operating systems as well, so it would be preferable to define a separate specification for them.
If a specification is established, we plan to publish it separately. Please contact the Ukadoc Project.
To indicate that the baseware is running, SSP holds a Mutex named "ssp", while Materia and CROW hold one named "sakura".
You can determine whether they are running at low cost by checking whether this named Mutex exists.
The state of the Mutex itself is unspecified, so there is no need to check whether it is signaled.
HANDLE hmutex = OpenMutex(MUTEX_ALL_ACCESS,FALSE,"ssp");
if ( hmutex ) {
// いる
CloseHandle(hmutex);
}
else {
//いない
}
All baseware hold a named file mapping object (FMO) while running.
Reading this interprocess shared memory allows you to obtain a list of running ghosts at relatively low cost.
To avoid reading incomplete information while it is being written, access is controlled by the FMO Mutex. Please check this as well.
\1]value[\r\n]\1] is the byte value 1, and [\r\n] is CR+LF (newline).ssp_fmo_header_00004468_000f0dea.path[\1]D:\ssp\
ssp_fmo_header_00004468_000f0dea.hwnd[\1]986602
ssp_fmo_header_00004468_000f0dea.name[\1]ラーシェ
ssp_fmo_header_00004468_000f0dea.keroname[\1]ティセ
ssp_fmo_header_00004468_000f0dea.sakura.surface[\1]0
ssp_fmo_header_00004468_000f0dea.kero.surface[\1]10
ssp_fmo_header_00004468_000f0dea.kerohwnd[\1]1052114
ssp_fmo_header_00004468_000f0dea.hwndlist[\1]986602,1052114
ssp_fmo_header_00004468_000f0dea.ghostpath[\1]D:\ssp\ghost\DE10_3001\
ssp_fmo_header_00004468_00120da6.path[\1]D:\ssp\
ssp_fmo_header_00004468_00120da6.hwnd[\1]1183142
ssp_fmo_header_00004468_00120da6.name[\1]Emily
ssp_fmo_header_00004468_00120da6.keroname[\1]Teddy
ssp_fmo_header_00004468_00120da6.sakura.surface[\1]20
ssp_fmo_header_00004468_00120da6.kero.surface[\1]10
ssp_fmo_header_00004468_00120da6.kerohwnd[\1]1117626
ssp_fmo_header_00004468_00120da6.hwndlist[\1]1183142,1117626,921002,2035340,658950
ssp_fmo_header_00004468_00120da6.ghostpath[\1]D:\ssp\ghost\emily4\
A key indicating the type of information and the information itself, as follows.
\0 side. Decimal notation.\1 side. Decimal notation.\1-side window. Decimal notation.[SSP only]\1]shiori:running,makoto-ghost:running\r\nFMO itself has no mechanism for exclusive control, so a separate Mutex is held to prevent read/write conflicts.
Its name is the FMO name + "FMO", for example:
FMO = Sakura : Mutex = SakuraFMO
FMO = SakuraUnicode : Mutex = SakuraUnicodeFMO
It is important to determine whether the Mutex is in a signaled or non-signaled state.
When reading or writing, always acquire ownership using WaitForSingleObject or an equivalent wait function, then release ownership with ReleaseMutex when finished.
Some older baseware may not support the Mutex for FMO, so failure to acquire the Mutex should not be treated as an error.
In this case, however, consider the possibility of obtaining an FMO during an incomplete write, and take care to write code that processes it as safely as possible.
//ベースウェア等の保持すべきアプリは代わりにCreateMutexを使う
HANDLE hMutex = OpenMutex(MUTEX_ALL_ACCESS,FALSE,"SakuraFMO");
//FMO用Mutexに対応していないベースウェアもあるので、見つからなかったら単にスキップ
bool isWaitSuccess = true;
if ( hMutex ) {
//INFINITEで待機すると永遠に待ち続けてGUIが止まるので適宜工夫すること
DWORD result = WaitForSingleObject(hMutex,INFINITE);
if ( result != WAIT_OBJECT_0 ) {
isWaitSuccess = false;
}
}
if ( isWaitSuccess ) {
//保持すべきアプリは代わりにCreateMutexを使う
HANDLE hFMO = OpenFileMapping(FILE_MAP_ALL_ACCESS,FALSE,"Sakura");
if ( hFMO ) {
char *pDataStart = static_cast<char*>(MapViewOfFile(hFMO,FILE_MAP_ALL_ACCESS,0,0,0));
if ( pDataStart ) {
//頭の4バイトはFMO最大サイズ。
//文字列終端(C言語文字列のゼロ終端)とは異なるので注意。
unsigned long length = *reinterpret_cast<unsigned long*>(pDataStart);
char *pData = pDataStart;
pData += 4;
//****************************************
//ここでpDataとlengthをつかってなにかやる
//****************************************
//MapViewOfFileの解除
UnmapViewOfFile(pDataStart);
}
//FMOハンドルを開放
//ベースウェア等の保持すべきアプリは開放せず持ち続けること
CloseHandle(hFMO);
}
}
if ( hMutex ) {
if ( isWaitSuccess ) {
//WaitForSingleObjectでMutexが非シグナル状態になるので、シグナル状態に戻す(Release)
ReleaseMutex(hMutex);
}
//最後にMutexのハンドルも要らないので開放
//ベースウェア等の保持すべきアプリは開放せず持ち続けること
CloseHandle(hMutex);
}