Create TOC

레이블이 Code Snippet인 게시물을 표시합니다. 모든 게시물 표시
레이블이 Code Snippet인 게시물을 표시합니다. 모든 게시물 표시

2009년 12월 1일

Python/자동 링크 생성

input에서 http 또는 https 링크를 찾아서 a tag를 붙여주는 python script

import re

re_href= re.compile('(\b(http|https)://([-A-Za-z0-9+&@#/%?=~_()|!:,.;]*[-A-Za-z0-9+&@#/%=~_()|]))')
output = re_href.sub(r'<a href="\1" target="_self">\1</a>', input)

2007년 11월 18일

To create a notification thread and then return

int WINAPI _tWinMain(HINSTANCE hinstExe, HINSTANCE, PTSTR pszCmdLine, int) {

    // Create our events for event logging notification as well as our event for ending the notification thread
    g_evtNewEventLogRecord = CreateEvent(NULL, TRUE, FALSE, NULL);
    HANDLE hThread = chBEGINTHREADEX(NULL, 0, EventNotifyThread, NULL, 0, NULL);

    DialogBox(hinstExe, MAKEINTRESOURCE(IDD_EVENTMONITOR), NULL, Dlg_Proc);

    // Clean up
    QueueUserAPC(DoNothingAPC, hThread, NULL);
    WaitForSingleObject(hThread, INFINITE);
    CloseHandle(g_evtNewEventLogRecord);
    CloseHandle(hThread);
    return(0);
}

///////////////////////////////////////////////////////////////////////////////
DWORD WINAPI EventNotifyThread(PVOID pvParam) {

    // Note that this is not the thread that called NotifyChangeEventLog.
    // The main thread does this in response to the WM_INITDIALOG message.
    // If the main thread were to terminate before this thread, then
    // notifications would cease to function. However, since it will always
    // terminate after this thread, then we can count on the notification event
    // working properly.

    // Wait for an event notification or for APC altering this thread to termiante.
    while (WaitForSingleObjectEx(g_evtNewEventLogRecord, INFINITE, TRUE) != WAIT_IO_COMPLETION) {
        FORWARD_WM_USERNOTIFYUPDATE(g_hwnd, PostMessage);
    }

    // We got an APC, this thread should terminate
    return(0);
}

2006년 3월 8일

인터넷 연결상태 알아오기


DWORD dwConnectionTypes;
if(InternetGetConnectedState(&dwConnectionTypes, 0)) // 정상적으로 검사됨
{
	if((dwConnectionTypes & INTERNET_CONNECTION_MODEM) != 0)
		printf("Internet connection using modem");

	if((dwConnectionTypes & INTERNET_CONNECTION_LAN) != 0)
		printf("Internet connection using LAN");

	if((dwConnectionTypes & INTERNET_CONNECTION_PROXY) != 0)
		printf("Internet connection using Proxy");

	if((dwConnectionTypes & INTERNET_CONNECTION_MODEM_BUSY) != 0)
		printf("Modem is busy");

	if((dwConnectionTypes & INTERNET_RAS_INSTALLED) != 0)
		printf("RAS is installed");

	if((dwConnectionTypes & INTERNET_CONNECTION_OFFLINE) != 0)
		printf("Offline");
}
else
	printf("InternetGetConnectedState() API is failed!");

파일 write시에 다른 프로세스가 접근하지 못하도록 파일을 lock

Original link

int f_lockwrite(char* szMsg, int iFileDesc)
{
	struct flock    *stFlock;
	int             n;

	stFlock = (struct flock*)malloc(sizeof(struct flock));
	stFlock->l_type = F_WRLCK;
	fcntl(iFileDesc, F_SETLKW, stFlock);
	if ((n = write(iFileDesc, szMsg, 1)) <= 0)
 	{
		return -1;
	}
	stFlock.l_type = F_UNLCK;
	fcntl(iFileDesc, F_SETLK, stFlock);
	free(stFlock);
	return 1;
}