|
I used LocalAlloc because that was what MS used in their example. The example is full of missed declarations and other problems; I've tried using the same data structure and memory allocation they do in the example, but populating it with the data from my class objects... that didn't work either.
I am now creating an object instance using the new operator as suggested...
MenuItem* myItem = new MenuItem;
myItem->SetValues(...);
...
MENUITEMINFO mii;
...
mii.dwItemData = (ULONG_PTR) myItem;
As before, I can access the object data when handling the WM_MEASUREITEM message but it's still crashing the application when using the exact same method when handling the WM_DRAWITEM message.
From what I understand, the memory allocated by new should remain allocated until I call delete . Please correct me if I'm wrong but based on that, I shouldn't need any additional allocation such as HeapAlloc...
|
|
|
|
|
I totally restructured my code in order to simplify things and try to track down what is wrong. My menu is now generated after getting the WM_CREATE message rather than dynamically when the user clicks for simplicity's sake. Each object is created using the new operator and destroyed using delete after receiving the WM_DESTROY message. I implemented a linked list and wrote a function to iterate through them at any time during the program's execution. The objects are all accessible. Since the LPDRAWITEMSTRUCT->itemData didn't seem to ever contain a valid pointer to my info, I now use my own function to retrieve my item data, and no longer load a pointer onto the dwItemData member of MENUITEMINFO.
My application is now drawing the menu for regular menu items: hooray! But I am still getting a nasty crash bug when the item being drawn is a submenu. In the class constructor, if an item is a submenu, an HMENU object is created and assigned to a member variable. The submenu is then created using the following code:
AppendMenu(myMenu, MF_POPUP | MF_OWNERDRAW, (UINT)menuItem->GetSubMenu(), menuItem->itemLabel);
When the MF_OWNERDRAW flag is set for a popup menu, all the members of the LPDRAWITEMSTRUCT seem to go haywire. All other menu items work fine. If I remove the MF_OWNERDRAW flag, all the submenus works perfectly. The sad thing is, I am still needing to use the itemID member of the LPDRAWITEMSTRUCT to determine which menu item is being drawn!
Am I missing something in my initialization? Or, is there another way to determine the ID of the menu item being drawn other than lpdis->itemID?
Thanks! Almost there...
[Edit]
Apparantly AppendMenu and SetMenuInfo doesn't quite it for initializing the LPDRAWITEMSTRUCT. Using InsertMenuItem did the trick.
modified on Wednesday, June 18, 2008 7:45 PM
|
|
|
|
|
Does anybody know how to read the actual position of desktop shortcuts and move them to a desired position.
many thanks in advance...
MS
|
|
|
|
|
error LNK2001: unresolved external symbol "public: static struct CRuntimeClass const C0_VIDEO_AVI_View::classC0_VIDEO_AVI_View" (?classC0_VIDEO_AVI_View@C0_VIDEO_AVI_View@@2UCRuntimeClass@@B)
Debug/CCC_Parser.exe : fatal error LNK1120: 1 unresolved externals
I am getting the above linker error in this line of code:
if(pWnd->IsKindOf( RUNTIME_CLASS( C0_VIDEO_AVI_View) ))
My C0_VIDEO_AVI_View class is derived from CView and has both runtime macros included.
IMPLEMENT_DYNCREATE(C0_VIDEO_AVI_View, CView)
DECLARE_DYNCREATE(C0_VIDEO_AVI_View)
If I delete DECLARE_DYNCREATE(C0_VIDEO_AVI_View) I get these errors
L:\0\0 Project\0 Project Template\V1\CCC_00\TabView_View.cpp(520) : error C2039: 'classC0_VIDEO_AVI_View' : is not a member of 'C0_VIDEO_AVI_View'
l:\0\0 project\0 project template\v1\ccc_00\framegrabbertestview1.h(16) : see declaration of 'C0_VIDEO_AVI_View'
L:\0\0 Project\0 Project Template\V1\CCC_00\TabView_View.cpp(520) : error C2065: 'classC0_VIDEO_AVI_View' : undeclared identifier
The C0_VIDEO_AVI_View was working OK until I tried to run RUNTIME_CLASS macro and I got his macro working fine in other classes.
I actually copied working code and just replaced the class in the RUNTIME_CLASS macro with C0_VIDEO_AVI_View and got this error.
As always, I appreciate your help and many thanks for reading.
Vaclav
|
|
|
|
|
does your C0_VIDEO_AVI_View class have a default constructor ? i think that's what the first linker error is complaining about.
|
|
|
|
|
Yes it does have a default constructor.
|
|
|
|
|
I'll try to run RUNTIME_CLASS macro some other way - after the class is constructed
|
|
|
|
|
See here[^] for details on the macros in question.
What seems to be going on is that you've correctly used DECLARE_DYNAMIC in the class definition but failed to use the IMPLEMENT_DYNCREATE macro in the .CPP file.
Steve
|
|
|
|
|
Not true,if I comment out //IMPLEMENT_DYNCREATE(C_Bitmap_View_, CView)I get the following linker errors.
Bitmap_View_.obj : error LNK2001: unresolved external symbol "public: virtual struct CRuntimeClass * __thiscall C_Bitmap_View_::GetRuntimeClass(void)const " (?GetRuntimeClass@C_Bitmap_View_@@UBEPAUCRuntimeClass@@XZ)
TabView_View.obj : error LNK2001: unresolved external symbol "public: static struct CRuntimeClass const C_Bitmap_View_::classC_Bitmap_View_" (?classC_Bitmap_View_@C_Bitmap_View_@@2UCRuntimeClass@@B)
Debug/CCC_Parser.exe : fatal error LNK1120: 2 unresolved externals
Error executing link.exe.
I have rebuild offending class from scratch and have no more linker errors related to RUNTIME_CLASS macro.
The remaining suspect is Gallery since the failing class was loaded from there. I had some weird problems with Gallery before.
Anyway, I do not have an energy to find out if the Gallery caused this problem.
Will try it later. Good night from Houston.
Vaclav
|
|
|
|
|
Hi,
I'm using VS2005 to create an ATL project which creates a simple blank window's application window embedded inside IE. I've followed the MSDN Browser helper object tutorial and code project examples to create a basic BHO and I've confirmed its registered with the browser.
Now I'd like to create a window inside the browser when its started. In my code below I think I have access to the window handle of the IE parent window. I've started looking at either Ccontainedwindow or CWindowimpl. Which class should I be using and how to create the embedded window? Or does anyone know of an example I could follow?
My class is as follows:
#include "stdafx.h"
#include "myBHO.h"
STDMETHODIMP CmyBHO::SetSite(IUnknown* pUnkSite)
{
if (pUnkSite != NULL)
{
pUnkSite->QueryInterface(IID_IWebBrowser2, (void**)&m_spWebBrowser);
}
else
{
m_spWebBrowser.Release();
}
if( m_spWebBrowser )
{
HWND hWnd;
m_spWebBrowser->get_HWND( (long*)&hWnd );
}
return IObjectWithSiteImpl<cmybho>::SetSite(pUnkSite);
}
</cmybho>
The Header file looks like:
#pragma once
#include "resource.h"
#include "overallBHO.h"
#include <shlguid.h>
#include <atlwin.h>
#include <windef.h>
#if defined(_WIN32_WCE) && !defined(_CE_DCOM) && !defined(_CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA)
#error "Single-threaded COM objects are not properly supported on Windows CE platform, such as the Windows Mobile platforms that do not include full DCOM support. Define _CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA to force ATL to support creating single-thread COM object's and allow use of it's single-threaded COM object implementations. The threading model in your rgs file was set to 'Free' as that is the only threading model supported in non DCOM Windows CE platforms."
#endif
class ATL_NO_VTABLE CmyBHO :
public CComObjectRootEx<ccomsinglethreadmodel>,
public CComCoClass<cmybho,>,
public IObjectWithSiteImpl<cmybho>,
public IDispatchImpl
{
public:
CmyBHO()
{
}
DECLARE_REGISTRY_RESOURCEID(IDR_myBHO)
DECLARE_NOT_AGGREGATABLE(CmyBHO)
BEGIN_COM_MAP(CmyBHO)
COM_INTERFACE_ENTRY(ImyBHO)
COM_INTERFACE_ENTRY(IDispatch)
COM_INTERFACE_ENTRY(IObjectWithSite)
END_COM_MAP()
DECLARE_PROTECT_FINAL_CONSTRUCT()
HRESULT FinalConstruct()
{
return S_OK;
}
void FinalRelease()
{
}
public:
STDMETHOD(SetSite)(IUnknown *pUnkSite);
private:
CComPtr<iwebbrowser2> m_spWebBrowser;
};
OBJECT_ENTRY_AUTO(__uuidof(myBHO), CmyBHO)
</iwebbrowser2></cmybho></ccomsinglethreadmodel></windef.h></atlwin.h></shlguid.h>
Thanks,
Chris
|
|
|
|
|
thats the code for BHO!, you have to create window and set IE as it parent, it will become visible in the IE
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow Never mind - my own stupidity is the source of every "problem" - Mixture
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
Support CRY- Child Relief and You/xml>
|
|
|
|
|
Hello friends,
I've a problem with the following code that does copy an exe and create a
new one with changed name.The program however copies the exe but the newly created exe can't be open.
ifstream fin;
char buf[1000000];
try
{
fin.open("C:\\Documents and Settings\\ritesh.mochi\\Desktop\\MFCBase64.exe",ios::in|ios::binary);
if(fin.is_open())
{
strcpy(buf,"");
while(!fin.eof())
{
char *ch=new char(1);
fin.read(ch,1);
strcat(buf,ch);
}
}
if(1)
{
unsigned long len;
len=strlen(buf);
ofstream fout;
fout.open("C:\\Documents and Settings\\ritesh.mochi\\Desktop\\MFCBase64_1.exe",ios::out|ios::binary);
fout.write(buf,len);
fout.close();
}
}
catch(...)
{
MessageBox("Exception occured.",_T("MFCBase64"),MB_OK);
}
ritz1234
|
|
|
|
|
The bug is you used strlen to get the file size. strlen stops when it reaches the first '\0' character inside the executable file (hence you possibly get a smaller size than the actual one).
BTW reading one chracter at time is poor programming in such a context.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
|
|
|
|
|
Thanks for the reply CPallini,
Actually when comparing the size the created new file has more size than the original file.
So,now i am maintaining a counter while reading a character from the file.
The code is shown below.
unsigned long len=0;
while(!fin.eof())
{
char *ch=new char(1);
fin.read(ch,1);
strcat(buf,ch);
len++;
}
And writing to the file as below
ofstream fout;
fout.open("C:\\Documents and Settings\\ritesh.mochi\\Desktop\\MFCBase641.exe",ios::out|ios::binary);
fout.write(buf,len-1);
fout.close();
Though the size of the source and destination exe is same in this case.I can't open this destination exe.Please help me.
ritz1234
|
|
|
|
|
ritz1234 wrote: char *ch=new char(1);
Why are you doing this? Heap memory is not necessary for what you are doing.
ritz1234 wrote: strcat(buf,ch);
This will not work when dealing with a single '\0' character.
"Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
I am doing this because I want to read file byte by byte.storing this bytes in the char buffer to write these bytes into the files.
ritz1234
|
|
|
|
|
ritz1234 wrote: I am doing this because I want to read file byte by byte.
Understandable, but extremely inefficient.
ritz1234 wrote: storing this bytes in the char buffer to write these bytes into the files.
Which does not explain why you are using heap memory. You are killing the memory manager for no good reason. Use a stack-based variable instead.
"Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
Thanks for the reply David,
Right now I am least concern about the efficiency of the code.This is not
the final code of the application.
Right now I am only concerning about the result of the program which is
not correct.
Please help me out.A sample code would be appreciated.
ritz1234
|
|
|
|
|
ritz1234 wrote: A sample code would be appreciated.
char ch;
int x = 0;
while (! fin.eof())
{
fin.read(&ch, 1);
buf[x++] = ch;
}
...
fout.write(buf, x - 1);
"Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
There are two problems with this code.
1. strcat: you cannot do a strcat with a single character since it expects both source and destination strings to be NULL terminated. Moreover, executable file is a binary file and this is going to create problems.
2. strlen: computes the length of a NULL terminated string so if a NULL character is encountered in between the buffer it will return incorrect length in case of binary files.
Try the following code. Here I have taken correct file size because you cannot have too large arrays on a stack.
char* buf = new char[2567672];
strcpy(buf, "");
int i =0;
ifstream fin("C:\\1.exe", ios::binary);
if(fin.is_open())
{
char ch;
while(!fin.eof())
{
fin.read(&ch, 1);
buf[i] = ch;
i++;
}
}
ofstream fout("C:\\2.exe", ios::binary);
fout.write(buf, i-1);
fout.close();
If you ask me then I would use following code:
size_t FileSize(std::ifstream* fp)
{
std::ifstream::pos_type begin = fp->tellg();
fp->seekg (0, std::ios::end);
std::ifstream::pos_type end = fp->tellg();
fp->seekg (0, std::ios::beg);
return end - begin;
}
void CopyBinaryFile()
{
ifstream fin("C:\\1.exe", ios::binary);
size_t _len = FileSize(&fin);
char* _buffer = new char[_len];
if(fin)
{
fin.read(_buffer, _len);
}
ofstream fout("C:\\2.exe", ios::binary);
if(fout)
{
fout.write(_buffer, _len);
fout.close();
}
}
Hope this solves your problem.
-Saurabh
|
|
|
|
|
Thanks for the help Subhas and David,
The problem is now resolved.
ritz1234
|
|
|
|
|
|
Hi All,
I am facing an issue relating to named pipes.
The OS used is Vista.
I am creating a named pipe within a service that is running in session zero. A client application tries to read through the pipe by calling api CreateFile. This fails since the client application is running in the user context.
Is there any way that this caould be overcome? Should i pass any securtiy attributes to the "CreateNamedPipe" or "CreateFile" apis?
Thanks in advance
Sunil
|
|
|
|
|
What error code are you getting?
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
It gives an error ACCESS_DENIED.
Sunil
|
|
|
|