|
Hi,
I want to raise an event from my code in vc++.
while raising the event i need to check for the null.
Basically to know whether some one has registered for this event . How the event can be checked for the Null value.
if it is not checked System::NullReference Exception is thrown
thanks in advance,
Sangeetha
|
|
|
|
|
try if ( event == nullptr )
rgds...mil10.
|
|
|
|
|
This gives the error as follows
error C3918: usage requires 'event' to be a data member
Regards
Sangeetha
|
|
|
|
|
seems u have no declared the evenet object properly. Please post the code.
rgds...mil10.
|
|
|
|
|
|
Here is the sample explaining why i need to check for a null.
Example:
public void delegate MyEventHandler();
public ref class MyClass
{
int i = 0;
public event MyEventHandler ^ MyEvent;
public SampleFunction
{
if(MyEvent!= nullptr) //raises C3918
{
MyEvent();
}
else
{
throw exp;
}
}
}
NOTE : I do register this event in some other class on certain conditions . So if it is not registered i need to throw it as a exception.
So it would be beeter if i check it for a null.
How can be this be done
Regards
Sangeetha
-- modified at 1:05 Monday 3rd April, 2006
|
|
|
|
|
hi,
As mentioned in those forums, it is impossible to check an event against nullptr in c++/cli but you could actually use a public delegate as data member instead.
regards Tobias
|
|
|
|
|
Hi,
immediately after telling you to use a delegate i found myself confronted with a similar problem.
Though i had to find a solution for myself as well. Basically in my case, i need to check a remote event for any dead delegates which are still connected to it. Otherwise it will end up in a remoting exception each time i'll raise the event. So i did the following:
public delegate void SensorUpdatedEventHandler (Object^, UpdateEventArgs^);
public ref class SensorWebLink : ...
{
...
SensorWeblink()
{
_RemotelyInvokedDelegates = nullptr;
}
...
...
private:
static SensorUpdatedEventHandler^ _RemotelyInvokedDelegates;
public:
event SensorUpdatedEventHandler^ SensorUpdatedEvent
{
void add(SensorUpdatedEventHandler^ pDelegate)
{
_RemotelyInvokedDelegates += pDelegate;
}
void remove(SensorUpdatedEventHandler^ pDelegate)
{
_RemotelyInvokedDelegates -= pDelegate;
}
void raise(Object^ pObject, UpdateEventArgs^ pEvtArgs)
{
if (_RemotelyInvokedDelegates)
{
_RemotelyInvokedDelegates->Invoke(pObject, pEvtArgs);
}
}
};
void RaiseSensorUpdatedEvent (UpdateEventArgs^ pEvtArgs);
};
void SensorWebLink::RaiseSensorUpdatedEvent(UpdateEventArgs^ pEvtArgs)
{
if(_RemotelyInvokedDelegates == nullptr)
{
Console::WriteLine("No listeners");
}
else
{
Console::WriteLine("Number of Listeners: {0}",_RemotelyInvokedDelegates->GetInvocationList()->Length);
SensorUpdatedEventHandler^ temporaryHandler = nullptr;
for each( Delegate^ del in _RemotelyInvokedDelegates->GetInvocationList())
{
try
{
temporaryHandler = (SensorUpdatedEventHandler^) del;
temporaryHandler(this, pEvtArgs);
}
catch( Exception^ e )
{
Console::WriteLine( e->ToString() );
_RemotelyInvokedDelegates -= temporaryHandler;
}
}
}
} Btw. I had this idea after reading this msdn article:
http://msdn2.microsoft.com/en-US/library/5f3csfsa(VS.80).aspx[^]
cheers Tobias
|
|
|
|
|
er... it's a kind of programing question but I though of asking it here as no one know the answer to this question out of all mainstream categories anyway....
I wrote a ManagedC++/.NET wrapper around Uniscribe. Which is the lowest level international text rendering (win32) API as I understand it.
With it I'm able to do custom rendering of formatted international text.
That's all good.
(For the curious who don't know it, you use various ScriptXXX to perform text analysis and then you finally render 'glyphs' (as opposed to characters) with ScriptTextOut)
Now come the problem, when I try to print.
I even manage to achieve perfect WYZIWIG behavior, that's cool and it works well on my work computer, on many customer's computer, on my home computer.
BUT, for some customer the preview is completely blank.
And for some other the printed page is completely blank as well.
What could it be?
How to fix that?
Any ideas?
Yep I have already (and unsuccessfuly) tried news://news.microsoft.com/microsoft.public.win32.programmer.international
-- modified at 23:31 Wednesday 29th March, 2006
|
|
|
|
|
I don't know much about what you are doing. However, I have had the same problem but it may or may not be related to yours. You may need to check the installed fonts. If the computer/printer cannot properly substitute your run-time font, a lot of times it just prints a blank page.
|
|
|
|
|
I should know... (the analysis function should tell me!)
Anyway that gave some related and unrelated idea... I'll try, thanks for the tip!
|
|
|
|
|
I'm trying to save the Time to the [char templog[256]]
it works perfectly on the developer pc. problem comes when running on client pc.
sprintf( templog, "\t%s ->Now Playing MP3::%s",<br />
DateTime::Now.ToString( L"T" ), advert );
output after saving to text file developer pc:
15:19:26 ->Now Playing MP3::C:\Media\MP3\Mike & The Mechanics - Word Of Mouth.mp3
output after saving to text file client pc:
È%¹y ->Now Playing MP3::C:\Media\MP3\Mike & The Mechanics - Word Of Mouth.mp3
both have .net 1.1 and .net 2.0 framework installed
both running Windows XP Home
both time & Date settings equal (including format settings)
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
code written in visual c++ .net 2003 Std edition.
any hints or help would be great thx in advance
dewald@silversquare.co.za
|
|
|
|
|
Hi Saber,
unfortunately I can't tell you, what is actually causing that behavior but I would start investigating in the following order:
1. try to use
sprintf( templog, "\t%s ->Now Playing MP3::%s",
DateTime::Now.ToString( L"T", DateTimeFormatInfo.InvariantInfo), advert );
and check also for any Globalization issues?, CultureInfo... etc.
2. check your character set or any formatting / encoding
3. give swprintf(...) a try
Hope that helps. Anyway it would be great if you could publish your result.
best regards
Tobias
|
|
|
|
|
Thx for the advice,
char tempbuf[128];<br />
sprintf( templog, "\t%s ->Now Playing FullScreen::%s::%s", <br />
_strtime(tmpbuf));
some times its better to stick to the basics. I've even checked the value
on client pc with gamehack the date works (also in log file) but the time is
giving problems.
_strtime(tmpbuf); is quicker and eisier to use any way...
thanx again for helping...
|
|
|
|
|
I see a couple problems with your code. First of all, "DateTime::Now.ToString( L"T" )" returns a Unicode string and sprintf does not handle Unicode. Secondly, you need to marshal or "pin" the managed heap string. The managed heap string can move without warning.
-- modified at 23:15 Wednesday 29th March, 2006
|
|
|
|
|
I have an app that works the following way:
1.the main process is written in C#
2.the main process uses class witten in Managed C++
3.in classes wirtten in Managed C++, Unmanaged C libs are called
4.as well the main process communicate with other host via .NET remoting
The other day a got a socket exception says (this happens at a very low frequency):
"An operation was attempted on something that is not a socket"
I investegated a lot and come to know this will only happen when the TCP/IP stack is destroyed
(IP addresses are all fixed)
I don't think TCP/IP stack can be destroyed by codes in C#. In Managed C++, all object reference created on C#'s side were
<br />
delete objRefCreatedInCSharp;<br />
in Dispose(). I am told this was done to prevent memory leak.
My qusetion is
1. Is delete necessary to prevent memory leak?
2. Won't this delete destroy TCP/IP stack at a certain timing?
Thank you very much.
-- modified at 0:35 Wednesday 29th March, 2006
|
|
|
|
|
You only use "delete" when you want to manually mark an object ready for disposal by the GC. If you prematurely use delete, of course, your code will not run properly. Since you did not provide any code examples, it is difficult to determine what your problem is.
|
|
|
|
|
I've got two header files:
// This is file1.h
#include "file2.h"
__gc class Class1
{
....
};
// This is file2.h
#include "file2.h"
__gc class Class2
{
....
}
I want both files to "know" each other, but it becomes an endless loop when i try to #include
each other... Any idia ?
Thanks
|
|
|
|
|
Correction about the file2.h file:
#include "file1.h"
of-course
|
|
|
|
|
Fundamental to the header files is the preprocessor. In order to avoid Multi-Inclusion you need a to tell the preprocessor to only include the header under certain conditions.
For example,
file1.h
<br />
#ifndef __FILE_1_H__<br />
#define __FILE_1_H__<br />
...<br />
#endif<br />
Now with that said, you probably need to revisit your header file design because you should not have a situation where File1 is including File2 and visa versa. If there is common definitions or structures, put those in a separate header. Keep the module specific data in the header that applies to the .CPP or .C module. You application might then look like:
file1.h
<br />
#include "File1.h"<br />
#include "CommonData.h"<br />
...<br />
file2.h would be similar.
Good Luck
Mike Luster
CTI/IVR/Telephony SME
|
|
|
|
|
Hi Virtek,
cross including seems kinda up to date problem. I would suggest that you add #pragma once to both of your header files.
#pragma once
"Specifies that the file will be included (opened) only once by the compiler when compiling a source code file."
Furthermore those two threads might be interesting to you as well.
http://www.codeproject.com/script/comments/forums.asp?msg=1401063&forumid=3785#xx1401063xx
http://www.codeproject.com/script/comments/forums.asp?msg=1422933&forumid=3785#xx1422933xx
regards Tobias
|
|
|
|
|
Do you know why C apply .h & .c file concept.
Becoz in .h file you do all declaration & in .c file you define all function.
so what you do for both header file make .c/.cpp file(which you already done.). & include file1.h in file2.cpp & file2.h in file1.cpp.
so it is not endless loop.
Divyang Mithaiwala
System Engineer & Software Developer
|
|
|
|
|
Divyang Mithaiwala wrote: so it is not endless loop.
It is. Unless you add the folowing
#ifndef _FILE1_H_ // At the beginning of .h file
#define _FILE1_H_ // Win32 style for declaring .h file
#endif // at the end of .h file
|
|
|
|
|
Hello all,
I have a strucure as given below.
public ref struct PASSTHRU_MSG
{
unsigned long ulProtocolID;
unsigned long ulRxStatus;
unsigned long ulTxFlags;
unsigned long ulTimeStamp;
unsigned long ulDataSize;
unsigned long ulExtraDataIndex;
array<unsigned char=""> ^ucData;//[4128];
};
PASSTHRU_MSG ^passThruMsg = gcnew PASSTHRU_MSG;
In Unmanaged Code we used to access the passThruMsg object of this type as (unsigned char*).
There we did not encounter any problem.
But now in Managed Code I'm supposed to convert passThruMsg object to array<unsigned char="">^) or (String^).
But I'm unable to convert it to either of the above mentioned forms.
I'm getting Compilation warning, and a runtime crash.
So, now I'm thinking of filling the data contained in the
passThruMsg object to a (String^) or an
(array<unsigned char="">^).
The issue with (String^) is String::Copy() doesn't work if it encounters a null character.
Please give me an advice which one to use.
Thanks in advance.
Regards
Sunil
|
|
|
|
|
Hi Sunil,
you could try one of these structs
public ref struct PASSTHRU_MSG
{
unsigned long ulProtocolID;
unsigned long ulRxStatus;
unsigned long ulTxFlags;
unsigned long ulTimeStamp;
unsigned long ulDataSize;
unsigned long ulExtraDataIndex;
static array <unsigned char>^ ucData = gcnew array <unsigned char>(4128);
};
or
public ref struct PASSTHRU_MSG
{
PASSTHRU_MSG()
{
ucData = gcnew array <unsigned char>(DATA_SIZE);
}
unsigned long ulProtocolID;
unsigned long ulRxStatus;
unsigned long ulTxFlags;
unsigned long ulTimeStamp;
unsigned long ulDataSize;
unsigned long ulExtraDataIndex;
array <unsigned char>^ ucData;
private:
literal int DATA_SIZE = 4128;
};
regards Tobias
|
|
|
|