本文整理汇总了C++中CreateMutexW函数的典型用法代码示例。如果您正苦于以下问题:C++ CreateMutexW函数的具体用法?C++ CreateMutexW怎么用?C++ CreateMutexW使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了CreateMutexW函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: Pimpl
Pimpl (String name, const int timeOutMillisecs)
: handle (0), refCount (1)
{
name = name.replaceCharacter ('\\', '/');
handle = CreateMutexW (0, TRUE, ("Global\\" + name).toWideCharPointer());
// Not 100% sure why a global mutex sometimes can't be allocated, but if it fails, fall back to
// a local one. (A local one also sometimes fails on other machines so neither type appears to be
// universally reliable)
if (handle == 0)
handle = CreateMutexW (0, TRUE, ("Local\\" + name).toWideCharPointer());
if (handle != 0 && GetLastError() == ERROR_ALREADY_EXISTS)
{
if (timeOutMillisecs == 0)
{
close();
return;
}
switch (WaitForSingleObject (handle, timeOutMillisecs < 0 ? INFINITE : timeOutMillisecs))
{
case WAIT_OBJECT_0:
case WAIT_ABANDONED:
break;
case WAIT_TIMEOUT:
default:
close();
break;
}
}
}
示例2: winpidgin_set_running
static BOOL winpidgin_set_running(BOOL fail_if_running) {
HANDLE h;
if ((h = CreateMutexW(NULL, FALSE, L"pidgin_is_running"))) {
DWORD err = GetLastError();
if (err == ERROR_ALREADY_EXISTS) {
if (fail_if_running) {
HWND msg_win;
printf("An instance of Pidgin is already running.\n");
if((msg_win = FindWindowExW(NULL, NULL, L"WinpidginMsgWinCls", NULL)))
if(SendMessage(msg_win, PIDGIN_WM_FOCUS_REQUEST, (WPARAM) NULL, (LPARAM) NULL))
return FALSE;
/* If we get here, the focus request wasn't successful */
MessageBoxW(NULL,
L"An instance of Pidgin is already running",
NULL, MB_OK | MB_TOPMOST);
return FALSE;
}
} else if (err != ERROR_SUCCESS)
printf("Error (%u) accessing \"pidgin_is_running\" mutex.\n", (UINT) err);
}
return TRUE;
}
示例3: CWE253_Incorrect_Check_of_Function_Return_Value__wchar_t_w32CreateMutex_15_bad
void CWE253_Incorrect_Check_of_Function_Return_Value__wchar_t_w32CreateMutex_15_bad()
{
switch(6)
{
case 6:
{
HANDLE hMutex = NULL;
hMutex = CreateMutexW(NULL, FALSE, NULL);
/* FLAW: If CreateMutexW() failed, the return value will be NULL,
but we are checking to see if the return value is INVALID_HANDLE_VALUE */
if (hMutex == INVALID_HANDLE_VALUE)
{
exit(1);
}
/* We'll leave out most of the implementation since it has nothing to do with the CWE
* and since the checkers are looking for certain function calls anyway */
CloseHandle(hMutex);
}
break;
default:
/* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
printLine("Benign, fixed string");
break;
}
}
开发者ID:gpwi970725,项目名称:testJuliet1,代码行数:25,代码来源:CWE253_Incorrect_Check_of_Function_Return_Value__wchar_t_w32CreateMutex_15.c
示例4: CreateAssemblyCache
/******************************************************************
* CreateAssemblyCache ([email protected])
*/
HRESULT WINAPI CreateAssemblyCache(IAssemblyCache **ppAsmCache, DWORD dwReserved)
{
IAssemblyCacheImpl *cache;
TRACE("(%p, %d)\n", ppAsmCache, dwReserved);
if (!ppAsmCache)
return E_INVALIDARG;
*ppAsmCache = NULL;
cache = HeapAlloc(GetProcessHeap(), 0, sizeof(IAssemblyCacheImpl));
if (!cache)
return E_OUTOFMEMORY;
cache->IAssemblyCache_iface.lpVtbl = &AssemblyCacheVtbl;
cache->ref = 1;
cache->lock = CreateMutexW( NULL, FALSE, cache_mutex_nameW );
if (!cache->lock)
{
HeapFree( GetProcessHeap(), 0, cache );
return HRESULT_FROM_WIN32( GetLastError() );
}
*ppAsmCache = &cache->IAssemblyCache_iface;
return S_OK;
}
示例5: apply_emet
void apply_emet(HMODULE pHandle, int a2)
{
// has this point
if ( setting_heapspray )
heapspray_apply();
if ( setting_bottomuprand )
bottomuprand_apply();
if ( setting_nullpage )
nullpage_apply();
if ( setting_dep )
enableDEP();
if ( setting_sehop ){
getModuleHandleEx(pHandle);
set_sehop_ceh_handler();
enable_sehop = 1;
}
if ( setting_eaf ){
hook_eat(L"kernel32.dll");
hook_eat(L"ntdll.dll");
HMODULE handle;
getModuleHandleEx(handle);
set_ceh_handler();
HANDLE thread_lock = CreateMutexW(0, 1, 0);
thread_count = 0;
ReleaseMutex(thread_lock);
CreateThread(0, 0, (LPTHREAD_START_ROUTINE)StartAddress, 0, 0, 0);
has_new_thread = true;
save_current_threadid();
}
}
示例6: WinMain
int APIENTRY WinMain(HINSTANCE hInstance
,HINSTANCE /*hPrevInstance*/
,LPSTR /*lpCmdLine*/
,int /*nCmdShow*/) {
auto exh = std::make_unique<google_breakpad::ExceptionHandler>( L".", nullptr, nullptr, nullptr, google_breakpad::ExceptionHandler::HANDLER_ALL, MiniDumpWithFullMemory, (const wchar_t*)nullptr, nullptr );
//int arg_c;
//auto arg_v = CommandLineToArgvW(GetCommandLine(), &arg_c);
auto hMutex = CreateMutexW(nullptr, TRUE, L"swtor_log_analizer_unique");
//WaitForSingleObject(hMutex, INFINITE);
/*
auto wnd = CreateDialogParamW(hInstance, MAKEINTRESOURCEW(IDD_MAIN_WINDOW), nullptr, DialogProc, 5);
ShowWindow(wnd,SW_NORMAL);
MSG msg
{};
while ( GetMessageW(&msg, wnd, 0, 0) ) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
DestroyWindow(wnd);*/
try {
app { APP_CATION, CONFIG_PATH }();
} catch ( const std::exception& e ) {
MessageBoxA(nullptr, e.what(), "Crash!", 0);
} catch ( ... ) {
MessageBoxA(nullptr, "Very bad crash", "Crash!", 0);
}
CloseHandle(hMutex);
}
示例7: main
int main(int argc, char* argv[])
{
DWORD dwError;
DWORD id1,id2;
AllocConsole();
InputHandle = GetStdHandle(STD_INPUT_HANDLE);
OutputHandle = GetStdHandle(STD_OUTPUT_HANDLE);
dprintf("Calling CreateMutex()\n");
hMutex = CreateMutexW(NULL, FALSE, L"TestMutex");
if (hMutex == INVALID_HANDLE_VALUE)
{
dprintf("CreateMutex() failed! Error: %lu\n", GetLastError());
return 0;
}
dprintf("CreateMutex() succeeded!\n");
hThread[0] = CreateThread(0, 0, thread1, 0, 0, &id1);
hThread[1] = CreateThread(0, 0, thread2, 0, 0, &id2);
dprintf("Calling WaitForMultipleObject()\n");
dwError = WaitForMultipleObjects(2, hThread, TRUE, INFINITE);
dprintf("WaitForMultipleObject() Error: %lu\n", dwError);
CloseHandle(hThread[0]);
CloseHandle(hThread[1]);
CloseHandle(hMutex);
dprintf("Main thread done!\n");
return 0;
}
示例8: gpgol_lock_spawning
/* Lock a spawning process. The caller needs to provide the address
of a variable to store the lock information and the name or the
process. */
gpg_error_t
gpgol_lock_spawning (lock_spawn_t *lock)
{
int waitrc;
int timeout = 5;
*lock = CreateMutexW (NULL, FALSE, L"spawn_gnupg_uiserver_sentinel");
if (!*lock)
{
log_error ("failed to create the spawn mutex: rc=%ld", GetLastError ());
return gpg_error (GPG_ERR_GENERAL);
}
retry:
waitrc = WaitForSingleObject (*lock, 1000);
if (waitrc == WAIT_OBJECT_0)
return 0;
if (waitrc == WAIT_TIMEOUT && timeout)
{
timeout--;
goto retry;
}
if (waitrc == WAIT_TIMEOUT)
log_error ("error waiting for the spawn mutex: timeout");
else
log_error ("error waiting for the spawn mutex: (code=%d) rc=%ld",
waitrc, GetLastError ());
return gpg_error (GPG_ERR_GENERAL);
}
示例9: m_acquired
SystemWideMutex::MutexData::MutexData(const wchar_t *mutex_name)
: m_acquired(0)
{
static const wchar_t prefix[] = L"csr_mutex_";
// this call previously used _alloca. It is probably better to do so,
// but I'm a coward when it comes to new things... this string gets
// deleted at the end of the function.
#ifdef MUTEX_LOG
enable_logging_ = (wcscmp(mutex_name, L"spi_app_mutex" ) == 0);
#endif
wchar_t * string = new wchar_t [ wcslen(mutex_name) + wcslen(prefix) + 1 ];
wcscpy(string, prefix);
wcscat(string, mutex_name);
wchar_t *sp = string;
while (*sp)
{
if (wcschr(L".\\/\"*", *sp))
*sp = L'-';
sp++;
}
handle = CreateMutexW(NULL, 0, string);
OUTPUT_HANDLE_CREATE(handle);
// OutputDebugString("Mutex created:");
// OutputDebugString(string);
// OutputDebugString("\n");
// int lasterr = GetLastError();
// assert(handle);
delete string;
}
示例10: wWinMain
int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow)
{
HANDLE hMutex;
INITCOMMONCONTROLSEX icex;
_wsetlocale(LC_ALL, L"JPN");
hInst = hInstance;
CreateConfigPath();
hMutex = CreateMutexW(NULL, FALSE, cnfmutexname);
if(hMutex == NULL || GetLastError() == ERROR_ALREADY_EXISTS)
{
return 0;
}
icex.dwSize = sizeof(icex);
icex.dwICC = ICC_LISTVIEW_CLASSES | ICC_TAB_CLASSES | ICC_PROGRESS_CLASS;
InitCommonControlsEx(&icex);
CreateProperty();
return 0;
}
示例11: _lockCount
MutexImpl::MutexImpl(MutexTypeImpl type)
: _lockCount(0)
, _recursive(type == MUTEX_RECURSIVE_IMPL)
{
_mutex = CreateMutexW(NULL, FALSE, NULL);
if (!_mutex) throw SystemException("cannot create mutex");
}
示例12: DoStartStartupItems
BOOL DoStartStartupItems(ITrayWindow *Tray)
{
DWORD dwWait;
if (!bExplorerIsShell)
return FALSE;
if (!s_hStartupMutex)
{
// Accidentally, there is possibility that the system starts multiple Explorers
// before startup of shell. We use a mutex to match the timing of shell initialization.
s_hStartupMutex = CreateMutexW(NULL, FALSE, L"ExplorerIsShellMutex");
if (s_hStartupMutex == NULL)
return FALSE;
}
dwWait = WaitForSingleObject(s_hStartupMutex, INFINITE);
TRACE("dwWait: 0x%08lX\n", dwWait);
if (dwWait != WAIT_OBJECT_0)
{
TRACE("LastError: %ld\n", GetLastError());
DoFinishStartupItems();
return FALSE;
}
const DWORD dwWaitTotal = 3000; // in milliseconds
DWORD dwTick = GetTickCount();
while (GetShellWindow() == NULL && GetTickCount() - dwTick < dwWaitTotal)
{
TrayProcessMessages(Tray);
}
if (GetShellWindow() == NULL)
{
DoFinishStartupItems();
return FALSE;
}
// Check the volatile "StartupHasBeenRun" key
HKEY hSessionKey, hKey;
HRESULT hr = SHCreateSessionKey(KEY_WRITE, &hSessionKey);
if (SUCCEEDED(hr))
{
ASSERT(hSessionKey);
DWORD dwDisp;
LONG Error = RegCreateKeyExW(hSessionKey, L"StartupHasBeenRun", 0, NULL,
REG_OPTION_VOLATILE, KEY_WRITE, NULL, &hKey, &dwDisp);
RegCloseKey(hSessionKey);
RegCloseKey(hKey);
if (Error == ERROR_SUCCESS && dwDisp == REG_OPENED_EXISTING_KEY)
{
return FALSE; // Startup programs has already been run
}
}
return TRUE;
}
示例13: _name
NamedMutexImpl::NamedMutexImpl(const std::string& name):
_name(name)
{
UnicodeConverter::toUTF16(_name, _uname);
_mutex = CreateMutexW(NULL, FALSE, _uname.c_str());
if (!_mutex)
throw SystemException("cannot create named mutex", _name);
}
示例14: socket_open
static int socket_open(int domain, int type, int protocol)
{
/* Translation constants to their Windows counterparts */
int win32_af = translate_address_family(domain);
if (win32_af < 0)
return win32_af;
int win32_type;
switch (type & LINUX_SOCK_TYPE_MASK)
{
case LINUX_SOCK_DGRAM: win32_type = SOCK_DGRAM; break;
case LINUX_SOCK_STREAM: win32_type = SOCK_STREAM; break;
case LINUX_SOCK_RAW: win32_type = SOCK_RAW; break;
case LINUX_SOCK_RDM: win32_type = SOCK_RDM; break;
case LINUX_SOCK_SEQPACKET: win32_type = SOCK_SEQPACKET; break;
default:
log_error("Unknown type: %d", type & LINUX_SOCK_TYPE_MASK);
return -L_EPROTONOSUPPORT;
}
socket_ensure_initialized();
SOCKET sock = socket(win32_af, win32_type, protocol);
if (sock == INVALID_SOCKET)
{
log_warning("socket() failed, error code: %d", WSAGetLastError());
return translate_socket_error(WSAGetLastError());
}
HANDLE event_handle = init_socket_event(sock);
if (!event_handle)
{
closesocket(sock);
log_error("init_socket_event() failed.");
return -L_ENFILE;
}
HANDLE mutex;
SECURITY_ATTRIBUTES attr;
attr.nLength = sizeof(SECURITY_ATTRIBUTES);
attr.lpSecurityDescriptor = NULL;
attr.bInheritHandle = TRUE;
mutex = CreateMutexW(&attr, FALSE, NULL);
struct socket_file *f = (struct socket_file *) kmalloc(sizeof(struct socket_file));
file_init(&f->base_file, &socket_ops, O_RDWR);
f->socket = sock;
f->event_handle = event_handle;
f->mutex = mutex;
f->shared = (struct socket_file_shared *)kmalloc_shared(sizeof(struct socket_file_shared));
f->shared->af = domain;
f->shared->type = (type & LINUX_SOCK_TYPE_MASK);
f->shared->events = 0;
f->shared->connect_error = 0;
if ((type & O_NONBLOCK))
f->base_file.flags |= O_NONBLOCK;
int fd = vfs_store_file((struct file *)f, (type & O_CLOEXEC) > 0);
if (fd < 0)
vfs_release((struct file *)f);
return fd;
}
示例15: XCECreateMutexW
HANDLE
XCECreateMutexW(LPSECURITY_ATTRIBUTES sa, BOOL io, LPTSTR name)
{
HANDLE hnd;
hnd = CreateMutexW(sa, io, name);
WCETRACE(WCE_SYNCH, "cmw: %p", hnd);
return(hnd);
}