|
In principle, DLL code is executed in the same thread as the EXE that invokes it, unless the DLL launches a secoondary thread on purpose.
Anyway, I'm curious about how you plan to terminate the llop using PeekMessage . Good luck.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
Obviously, when I work in a executable my code seems like this:
fct()
{
.... init loop ...
MSG msg;
while(!(::PeekMessage(&msg, GetDlgItem(IDC_STOP)->m_hWnd, 0, 0, PM_NOREMOVE))
{
...... do a step ...
}
}
where IDC_STOP is a button.
If I put the same code in a DLL
fct()
{
.... init loop ...
MSG msg;
while(!(::PeekMessage(&msg, 0, 0, 0, PM_NOREMOVE))
{
...... do a step ...
}
}
The GUI freezes and I have to use the task manager to kill the task
Thanks,
Gelu
|
|
|
|
|
Hi,
First create a new thread for your endless loop function. Then the first line of the thread execute the following
[code]
PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE)
[/code]
This will force the OS to create a non windowed message loop. Inside your loop you can the use PeekMessage or GetMessage to receive messages for that thread. Obviously the hWnd member of the MSG struct will contain NULL because is has no window.
To post a message to the thread message loop use PostThreadMessage. This will fail if you haven't created the message loop at the start of the thread
Hope this helps
Ceri
|
|
|
|
|
Hi Ceri,
My experience on creating threads in Windows system is very limited. Can you recommend me some code?
Thanks,
Gelu
|
|
|
|
|
Try this
[code]
#include "stdafx.h"
#define CUSTOMSTOPMSG WM_USER + 1
DWORD WINAPI ThreadProc(LPVOID lpParameter);
void StartThread();
void StopThread();
//the thread ID. Must give this ID to PostThreadMessage
DWORD dwThreadID = 0;
//this function is the thread function - contains the infine loop
DWORD WINAPI ThreadProc(LPVOID lpParameter)
{
MSG msg;
int iRetVal = 1;
PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE); //forces creation of non windowed message loop
while(!(PeekMessage(&msg, NULL, CUSTOMSTOPMSG, CUSTOMSTOPMSG, PM_REMOVE)))
{
//....do processing here
};
//.... do clean up here
return(iRetVal);
}
//execute this function from your standard thread
void StartThread()
{
HANDLE hThread;
//you can use this to pass a parameter to your thread
//(usualy a pointer to a struct or some other kind of user data)
LPVOID pParam = NULL;
hThread = CreateThread(NULL,0,ThreadProc,pParam,0,&dwThreadID);
}
void StopThread()
{
//obviously wParam and lParam in the PostThreadMessage could be some value also
if(dwThreadID)
PostThreadMessage(dwThreadID,CUSTOMSTOPMSG,0,0);
}
[/code]
Call StartThread when you want to initialize the loop and StopThred when you want to stop (I guess you would use your button for this)
Let me know how you get on
|
|
|
|
|
Many thanks Ceri. It works perfectly.
Gelu
|
|
|
|
|
|
I'd like to create a control (a ComboBox) and position it at the bottom of a window. Thus I need to know the height of this control before I create it.
If I just measure it on this PC and code it as a const, then it will look ugly if someone has a bigger font or s.th else is changed.
Is there any way to get the standard size of such a control without creating it first ? (s.th. like GetSystemMetrics for controls)
|
|
|
|
|
I couldn't find any function equivalent to GetSystemMetrics for combo boxes, but I would try this technique:
1. Create a temporary combo box without the WS_VISIBLE flag, so that it's invisible.
2. Retrieve its height. GetWindowRect should do it.
3. Destroy it.
Regards,
Alvaro
All you need in this life is ignorance and confidence, and then success is sure. -- Mark Twain
|
|
|
|
|
Works just like it should
thank you.
|
|
|
|
|
Hi all,
I stumbled upon an interesting character in a csv file created by Excel. the character's decimal representation is -96 and hex is FFFFFFA0. Does anybone know from which character set this one jumped out?
Thanks in advance.
Alberto Gattegno
Software Engineer
http://www.itgil.com
|
|
|
|
|
Could it be á, i.e 0xA0 or 0xFF - 0xA0, 0c5F '_'
If I have seen further it is by standing on the shoulders of Giants. - Isaac Newton 1676
|
|
|
|
|
Hi
I've always read that with every "new" there shld be a "delete" and likewise with "malloc" and "free", otherwise it'll lead to memory leak.However when the application is terminated, will this resources be freed?
Once, i've created an object with "new" in a loop and was unable to delete the object outside the loop.Anyone knows why?
thks!
|
|
|
|
|
raner wrote:
However when the application is terminated, will this resources be freed?
Yes. In debug mode, the IDE shows nasty comments about the leaks, but thats it.
So, if your app starts, runs a second, leaking a few KB of memory and terminates, no harm is done.
But if you imagine MS-Word would leak one byte per keystroke, you see how this would lead to trouble.
Basically, dont leak at all! It is not that hard to acomplish if you are careful:
Drop char* in favor of std::string or CString, drop dynamic C-arrays in vavor of std::vector etc.
Those pointers that you need are initialised at creation, checked against NULL before use and set to NULL after the delete.
raner wrote:
Once, i've created an object with "new" in a loop and was unable to delete the object outside the loop.Anyone knows why?
You threw away the pointer to this object, eg. overwriting it in the next loop iteration?
Remember: My opinions may have changed, but not the fact that I am right.
|
|
|
|
|
jhwurmbach wrote:
Drop char* in favor of std::string or CString, drop dynamic C-arrays in vavor of std::vector etc.
By std::vector, are u referring to STL?..Sorry i'm not very familiar with VC yet.
For the use of "new" in the loop, yes, i reassigned the pointer to consecutive elements in an array at each iteration.Do i not need to delete the pointer then?
thks alot!
|
|
|
|
|
Yes std:: is the namespace where the STL resides.
Read the tutorials here at CP, and you will understand how it works, and soon be a follower of the GREAT STL *manic laugh*
As to the loop, you said you could not delete memory you new ed in a loop.
If you have overwritten the pointer to this memory (being your only access) in the next iteration, you can not delete it: A delete on your pointer deletes only the last new ed memory.
My opinions may have changed, but not the fact that I am right.
|
|
|
|
|
ok..i see the point now.
But can i confirm that the following statements should be fine?
int* pi = 0;
for(int i = 0; i < 100;++i)
{
pi = new int;
//I'll store pointer pi of each iterationin some kind of array, so it can be retrieved later
}
delete pi;
|
|
|
|
|
raner wrote:
But can i confirm that the following statements should be fine?
[..example deleted..]
No! You have 100 'new int ' instructions (in the loop), but only one delete (after the loop). You are leaking 99 int s.
This one works:
#include < iostream >
#define num 100
int main(int argc, char* argv[])
{
typedef int* intPtr;
intPtr* pI = new intPtr[num];
for(int i = 0; i < num; ++i)
{
pI[i] = new int;
*pI[i] = i*i;
}
for( i = 0; i < num; ++i)
std::cout << "i: " << i << " i quadrat: " << *pI[i] << std::endl;
for(i = 0; i < num; ++i)
{
delete pI[i];
}
delete[] pI;
return 0;
}
My opinions may have changed, but not the fact that I am right.
|
|
|
|
|
i think i get it...thks alot!
|
|
|
|
|
The best way to understand the difference is to analyze what new/delete is actually doing.
new(global new operator):
1. Call operator new of the data type to allocate n bytes of memory. Default implementation just calls ::malloc(n)
2. Call constructor of the given data type for the new object
delete (global delete operator):
1. Call destructor of the given data type for the object being deleted
2. Call operator delete of the data type to free the memory. The default implementation just calls ::free(p).
Example
main()
{
A* pA = new A();//1. call ::new
//2. call A::operator new(...)
//3. call A::A()
delete pA; ;//1. call ::delete
//2. call A::~A
//3. call A::operator delete(...)
}
The array and build in types are slightly different, but we can ignore the difference for now.
Now about loop allocation
int* pi = 0;
for(int i = 0; i < 100;++i)
{
pi = new int;//we allocate new int
//if we do not call delete before next iteration
//we are going to loose memory when pi is
//reassign to new value
}
I hope it helps
AlexO
P.S. I did not cover constructor/destructor invokation order, I assume you already know about that
|
|
|
|
|
The following example inserts a text (L"Los Angeles") into the bookmark "[City]" in the word.
please, how to pass the text with CString variable instead of (L"text"). That is I need to insert a variable
and please, How to call a "SaveAs" command of the MSWord.
thanx.
void CWordAutoView::OnWordautomation()
{
if (!AfxOleInit())
{
AfxMessageBox("Could not Create an Automation");
return;
}
VARIANT root[64] = {0}; // Generic IDispatchs
VARIANT parm[64] = {0}; // Generic Parameters
VARIANT rVal = {0}; // Temporary result holder
int level=0; // Current index into root[]
// Initialize the OLE Library...
OleInitialize(NULL);
// 1:
VARIANT Sub = {0};
VariantCopy(&Sub, &rVal);
// 2:
VARIANT wordApp = {0};
// 3:
VARIANT wordDoc = {0};
// 4:
VARIANT wordRange = {0};
// 5:
{
CLSID clsid;
CLSIDFromProgID(L"Word.Application", &clsid);
HRESULT hr = CoCreateInstance(clsid, NULL, CLSCTX_LOCAL_SERVER|CLSCTX_INPROC_SERVER, IID_IDispatch, (void **)&rVal.pdispVal);
if(FAILED(hr)) {
char buf[256];
sprintf(buf, "CoCreateInstance() for \"Word.Application\" failed. Err=%08lx", hr);
::MessageBox(NULL, buf, "Error", 0x10010);
_exit(0);
}
rVal.vt = VT_DISPATCH;
}
VariantCopy(&wordApp, &rVal);
VariantClear(&rVal);
// 6: Open D:\\Wordtest.doc
VariantCopy(&root[++level], &wordApp);
AutoWrap(DISPATCH_PROPERTYGET|DISPATCH_METHOD, &root[level+1], root[level++].pdispVal, L"Documents", 0);
parm[0].vt = VT_BSTR; parm[0].bstrVal = ::SysAllocString(L"D:\\Wordtest.doc");
AutoWrap(DISPATCH_PROPERTYGET|DISPATCH_METHOD, &rVal, root[level].pdispVal, L"Open", 1, parm[0]);
VariantClear(&parm[0]);
VariantClear(&root[level--]);
VariantClear(&root[level--]);
VariantCopy(&wordDoc, &rVal);
VariantClear(&rVal);
// 7:
rVal.vt = VT_I4;
rVal.lVal = 1;
VariantCopy(&root[++level], &wordApp);
AutoWrap(DISPATCH_PROPERTYPUT, NULL, root[level].pdispVal, L"Visible", 1, rVal);
VariantClear(&root[level--]);
VariantClear(&rVal);
// 8:
VariantCopy(&root[++level], &wordDoc);
AutoWrap(DISPATCH_PROPERTYGET|DISPATCH_METHOD, &rVal, root[level].pdispVal, L"Goto", 1, parm[0]);
VariantClear(&parm[0]);
VariantClear(&root[level--]);
VariantCopy(&wordRange, &rVal);
VariantClear(&rVal);
// 9:
CString city;
city = "MyCity";
VariantCopy(&root[++level], &wordRange);
parm[0].vt = VT_BSTR; parm[0].bstrVal = ::SysAllocString(L"LOS Angeles");
AutoWrap(DISPATCH_METHOD, NULL, root[level].pdispVal, L"InsertAfter", 1, parm[0]);
VariantClear(&parm[0]);
VariantClear(&root[level--]);
// 10: Save
VariantCopy(&root[++level], &wordDoc);
AutoWrap(DISPATCH_METHOD, NULL, root[level].pdispVal, L"SaveAs", 0);
VariantClear(&root[level--]);
// 11:
VariantClear(&wordApp);
// 12:
// Clearing variables
VariantClear(&Sub);
VariantClear(&wordApp);
VariantClear(&wordDoc);
VariantClear(&wordRange);
// Close the OLE Library...
OleUninitialize();
}
|
|
|
|
|
You could use the string conversion macro T2W which converts an LPTSTR to a LPWSTR.
Remember to specify the USES_CONVERSION macro at the start of your function.
For example:
void func( LPTSTR lpsz )
{
USES_CONVERSION;
...
LPWSTR x = T2W(lpsz)
// Do something with x
...
}
|
|
|
|
|
I want to develop a firewall but I want to block packets on the base of MAC address. The problem is when I capture a packet on network layer using winsock the MAC address is already stripped off. We can use ARP but it will increase the network traffic and would be difficult to handle. Plz if u have other options tell me or make the use of ARP clear to me as I am a beginner. Refer me relevent sites/links/libraries/api's e.t.c.
Reply me soon
|
|
|
|
|
If you want to firewall at the network level you can't do it through Winsock. Winsock is to high on the stack to be useful as the etherframes are stripped before you get it, and you should move down to the TDI or NDIS layer in kernel mode. There are plenty of examples for this, and the latest XP SP1 DDK has an example of such a driver. You could modify it to suit (as I don't believe it actually touches the MAC).
As a starting point, try going to: http://www.ndis.com/papers/winpktfilter.htm
It is my understanding that PCAUSA provides a lot of infrastructure and documentation on their own NDIS driver source code which you can purchase. You can start reading about it over here: http://www.rawether.net/ndisim/
Have fun,
SilverStr
|
|
|
|
|
dear all,
i get an error in msdev.exe whenever i try to debug my code in windows xp or win 2K.
but i am able to debug in win 98 and win me.
is there some other way for debugging in windows Xp, 2k and NT.
plz. help!
<marquee width="150">rishabhs
|
|
|
|
|