|
a = new TCHAR [nLen + 1]; //mistake #1
delete [] a; // mistake #2
Tim Smith
I'm going to patent thought. I have yet to see any prior art.
|
|
|
|
|
Thank you for reply.
How to modify it?
-Freehawk
|
|
|
|
|
Well, you're trashing your stack due to the new[] bug that's been pointed out.
After you fix that, the problem becomes one of where is the memory being allocated? Unless you can be sure that both modules are using the same heap, you can't alloc in one module and free in the other. Because MFC has its own spiffy memory management, it probably has its own heap(s).
The way I usually do it is to alloc with CoTaskMemAlloc() and free with CoTaskMemFree() . Those use an allocator that the entire process can access.
[edit]Actually, it's not doing anything to the stack, since the memory is on the heap. It was late when I wrote that The code does write past the end of an allocated block of memory, though.[/edit]
--Mike--
Personal stuff:: Ericahist Updated Feb 6! | Homepage
Shareware stuff:: 1ClickPicGrabber | RightClick-Encrypt
CP stuff:: CP SearchBar v2.0.2 | C++ Forum FAQ
Actual sign at the laundromat I go to: "No tinting or dying."
|
|
|
|
|
Ack, I didn't even notice that Michael, thanks.
Tim Smith
I'm going to patent thought. I have yet to see any prior art.
|
|
|
|
|
try:
LPTSTR a; //TCHAR* a
...
delete[] a;
|
|
|
|
|
There are some ATL templates and macros that help catch COM events.
As IDispEventSimpleImpl, SINK_ENTRY_INFO etc.
But what I must to do if I do not know all events before a program start.
For example, there are Outlook::ItemsEvent for Outlook folders. I can catch any one. But I want catch all to one function. How can I register events dynamically?
Your ideas?
================================
My home is here!
|
|
|
|
|
Brigsoft wrote:
But what I must to do if I do not know all events before a program start.
Then you're in for a rough ride. COM events are either tied to a vtable entry (non-dispinterface source) or to a DISPID (dispinterace source). In both cases, the events are tied to an interface ID. You must know in advance what you're dealing with. Of course, one could devise a more dynamic homebrew event scheme - but alas, that's not the case here. COM events are as static as stone.
Brigsoft wrote:
But I want catch all to one function.
You can do this quite easily if the source interface is a dispinterface. Simply implement the dispinterface "manually" in a separate class like this:
template <typename T, typename SourceInterface, int ID>
class CSourceInterfaceImpl {
public:
HRESULT Init() {
...
}
void Finalize() {
}
STDMETHOD(GetIDsOfNames)(...) {
}
STDMETHOD(GetTypeInfo)(...) {
}
STDMETHOD(GetTypeInfoCount)(...) {
}
STDMETHOD(Invoke)(...) {
return static_cast<T>(this)->EventInvocation(
ID,
...
);
}
};
#define ID_FOR_EVENTS_X 1
#define ID_FOR_EVENTS_Y 2
#define ID_FOR_EVENTS_Z 3
class ATL_NO_VTABLE CMyCOMClass :
public ...,
public CSourceInterfaceImpl<CMyCOMClass, ISourceX, ID_FOR_EVENTS_X>,
public CSourceInterfaceImpl<CMyCOMClass, ISourceY, ID_FOR_EVENTS_Y>,
public CSourceInterfaceImpl<CMyCOMClass, ISourceZ, ID_FOR_EVENTS_Z>
{
...
HRESULT FinalConstruct() {
HRESULT hr;
hr = CSourceInterfaceImpl<CMyCOMClass, ISourceX, ID_FOR_EVENTS_X>::Init();
hr = CSourceInterfaceImpl<CMyCOMClass, ISourceY, ID_FOR_EVENTS_Y>::Init();
hr = CSourceInterfaceImpl<CMyCOMClass, ISourceZ, ID_FOR_EVENTS_Z>::Init();
}
void FinalRelease() {
CSourceInterfaceImpl<CMyCOMClass, ISourceX, ID_FOR_EVENTS_X>::Finalize();
CSourceInterfaceImpl<CMyCOMClass, ISourceY, ID_FOR_EVENTS_Y>::Finalize();
CSourceInterfaceImpl<CMyCOMClass, ISourceZ, ID_FOR_EVENTS_Z>::Finalize();
}
HRESULT EventInvocation(int nId, ... ) {
switch(nId) {
case ID_FOR_EVENTS_X:
break;
case ID_FOR_EVENTS_Y:
break;
case ID_FOR_EVENTS_Z:
break;
default:
ATLASSERT(false);
return E_NOTIMPL;
}
}
}
Caveat Emptor; I have not tried this in a compiler. As you can see, I've left a lot of implementation details out of the picture. But I think the "theory" is sound, and it achieves what you are requesting. Filling in the missing pieces should be trivial if you look up the functions I mention in MSDN and have at least some experience with COM/IDispatch.
Did I make any sense? Did I understand your problem (at all?)
--
Gott weiß ich will kein Engel sein.
|
|
|
|
|
Suppose i have got a struct with vectors inside
<code>
struct A
{
int a;
vector<int> b;
};
vector <A> b;
Is this safe or do i have to write a copy contructor ?
"Just looking for loopholes." W. C. Fields American actor, 1880-1946, explaining why he was reading the Bible on his deathbed.
|
|
|
|
|
Perfectly safe. The compiler will automatically generate a copy constructor using what's defined by the aggregated objects.
Generally, a copy constructor is only needed if the structure contains pointers, and aliasing them would be a bad ideatm.
Imagine this scenario:
struct S {
int* p;
S() { p = new int[10]; }
~S() { delete [] p; }
};
{
S a;
S b(a);
}
In cases like this it's necessary to write your own copy constructor which either a) makes a complete copy of the array inside the object being copied from, or b) you implement some reference counting mechanism. There are gigantic semantic differences between the two cases, as you may understand. It's basically a question of "by value" or "by reference". If I chose b), any modification to let's say a.p[0] would be visible in b.p[0] .
In your example, std::vector has a well defined copy constructor. By the way, std::vector implements "by value" semantics. Anyway, the C++ compiler will always automatically generate a copy constructor if, and only if, you omit one. The generated code would look something like this:
A::A(const A& other) : a(other.a), b(other.b) { } . And how a and b are copied is defined by their copy constructors.
--
Gott weiß ich will kein Engel sein.
|
|
|
|
|
I need to show more than two views in a sdi frame window.
My code like this:
<br />
RECT rc;<br />
GetClientRect(&rc);<br />
m_hWndClient = m_view.Create(m_hWnd,rc);<br />
m_view.ShowWindow(SW_SHOW);<br />
m_view.SetFocus();<br />
m_view2.ShowWindow(SW_HIDE);<br />
It can show m_view2,but not like default view.
How can I switch views correctly??
opening view with wild mind
|
|
|
|
|
You need to call Create() on m_view2. Then, in your code that switches the view, you need to hide m_view, show m_view2, and then set m_hWndClient to m_view2.
|
|
|
|
|
To add to what TFrancis said, after setting m_hWndClient , call UpdateLayout() to make sure the view is sized correctly to fit the client area of the frame.
--Mike--
Personal stuff:: Ericahist Updated Feb 6! | Homepage
Shareware stuff:: 1ClickPicGrabber | RightClick-Encrypt
CP stuff:: CP SearchBar v2.0.2 | C++ Forum FAQ
"That probably would've sounded more commanding if I wasn't wearing my yummy sushi pajamas."
-- Buffy
|
|
|
|
|
Hi!
I am starting from ground up. I am with simple Win32 Application and I want add ATL functionality in it by fooling wizard. I planned:
1)a cpp file that has the same name as the project .
2)an ATL object map (the map can be empty, but it must be present).
3)an IDL file with the same name as the project and this file must have a Library block.
Need feedback on 2 and 3.
|
|
|
|
|
What kind of feedback?
That's how to fool the wizard. The easiest way to get examples of (2) and (3) if that's what you need is to run the VC++ AppWizard and select ATL. That will generate you the appropriate files, and you can cut and paste from there.
I've done this before, and it works.
Steve S
|
|
|
|
|
Problem with (3) is with
the entire IDL file.As it should not compile. So What modifications should I do in the project settings when I add it to the project.
And working on Ur copy paste tip. What should be the type (EXE or DLL )of ATL WORKSPACE.
Thanx for all that feedback. And feedback always for learning.
|
|
|
|
|
The IDL file can be as simple as this;
import "oaidl.idl";
import "ocidl.idl";
[
uuid(FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF),
version(1.0),
helpstring("myapp library")
]
library MyApp
{
importlib("stdole32.tlb");
importlib("stdole2.tlb");
};
You don't even need to use a different UUID, since you're not going to be registering it.
Your 'dummy' can be either, I tend to use DLL out of habit, since I write in-proc servers quite often
Steve S
|
|
|
|
|
Hello Everyone
Could you tell me How to launch a console simply from a wtl project?(Only launch)
Thank you.
-Freehawk
|
|
|
|
|
I'm using a CTreeViewCtrl in my project. It works great except for one small problem. When I change the system UI appearance to something like "Plum" so that the background color changes the tree has problems. When you expand an item in the tree sometimes the newly visible items are drawn with a white background. Is there a workaround?
I'm using Win2000, VS .Net 2002, and WTL ver 7.0. I'm kinda in the middle of the project so I figured I'd finish it before switching over to WTL 7.1. Is this something that was fixed in 7.1 and I should just switch over to it?
|
|
|
|
|
Hi
I try to insert in ATL dialog my oneself ActiveX object (
ATL composite control) but wizard not found this one(in dialog box of wizard i not see it), althought when i insert it from Add to project -> Components & Control wizar i see one into list.Why???I can't use Add to project -> Components & Control wizard because of my ATL dialog allocated in ATL project without MFC support.
Thanks
|
|
|
|
|
Looks like your activex is not registered
Edward.
|
|
|
|
|
My interface member is a BSTR Sum.
<br />
{<br />
BSTR bBuff = 0;<br />
SysAllocStringLen(bBuff,20);<br />
m_Sum = _itow((Num1 + Num2),bBuff,10);<br />
return S_OK;<br />
}
It's cilent is a WIn32 Application. I just want to TextOut this BSTR.How to do it?
One more thing if I want to pass data from ATL server to Client, how to do it using methods. I am straightaway using Interface data member for it.
|
|
|
|
|
I'm guessing this is not a UNICODE build
If this isn't an option, then you can use various methods to convert the BSTR to a regular char 'string'.
There's an atlconv.h which you can include, then have code like this;
{
USES_CONVERSION;
TCHAR* p = W2T( bBuff );
TextOut(....);
}
Alternatively if your client uses MFC you can use
CString tmp( yourBSTR );
since CString has a conversion method.
Another alternative is to use WideCharToMultiByte to do the conversion. (It's what the conversion macros in atlconv.h use, I believe...)
Steve S
|
|
|
|
|
Thanx. that was a great help to me. I am using Win32 SDK. Is development in Win32SDK has more overhead then that of MFC?
I use WIn32SDK because I am able to understand the flow, while I tried MFC at times and it seems to be overwhelming.
|
|
|
|
|
Development can take longer using raw SDK. Depends how well you 'grok' the framework. I tend to use WTL rather than MFC, unless I have a particular need for MFC specific features, such as support for OLE compound documents.
In many cases, the framework obscures your view of what's happening, until you understand it, and then it accelerates development; class = code you don't have to write.
Lots of my test apps though are pure SDK, but short.
Steve S
|
|
|
|
|
Even eaiser:
::TextOutW(...)
No need for conversion at all
Edward
|
|
|
|