|
if you are writing C code for windows platform then you can user __try, __leave and __finally combination to avoid goto.
http://msdn.microsoft.com/en-us/library/9xtt5hxz(v=vs.80).aspx[^]
the main reason to avoid goto is the the instruction set directly jumps to given location and hence you do not get the call stack once you have jumped to new location. It would be really difficult if you have to debug some code like this.
using conitnue is always acceptable it is very useful in certain scneraois when you want to skip some of the values and continue with rest of them for certain operation which is running in a for or while loop and this does not have issue like goto where callstack will be lost.
|
|
|
|
|
continue isn't on quite the same level of abusive potential as goto : goto can jump backwards, out of multiple nested levels of scope, and into an entire different scope. If there are more than a couple of lines or scopes between the goto and the associated label, maintaining the code after several changes is painful at the very least, and hazardous at best.
continue on the other hand only jumps forward, to the end of the current scope. That's pretty much the same as break within a switch statement. it doesn't break the flow of control as indicated by existing control statements (if , for , do , etc.).
|
|
|
|
|
<pre lang="I am trying to gain “remote” access to DirectShow graph.
The graph is in Running Object Table under name.
I can retrieve / enumerate the ROT and get the named moniker.
However, my call to BindToObject fails.
Basically, I am not sure if I have the call to this method set properly.
My question is – since I am looking for a graph interface – do I need to pass graph interface or can I use Iunknown? It fails both ways.
I am nor really sure how to get the graph to the MFC document, but that is next step.
I am enclosing my code snippets , please note - it is under deconstruction and full of notes / comments.
I like it that way.
Any help will be as always appreciated.
Cheers
Vaclav
"></pre>
TRACE("\nHRESULT CDS::C_AddGraphToRot(IUnknown *pUnkGraph, DWORD *pdwRegister)");
IMoniker * pMoniker;
IRunningObjectTable *pROT;
IEnumMoniker *pMonikersTable;
IMoniker *pCurrentMoniker;
int iTestCount = 0;
WCHAR wsz[128] = L"Video" ; // MN used to retrive
HRESULT hr;
if (!pUnkGraph || !pdwRegister)
{
TRACE("\n!pUnkGraph || !pdwRegister");
return E_POINTER;
}
TRACE("\nGetRunningObjectTable ");
if (FAILED(GetRunningObjectTable(0, &pROT)))
{
TRACE("\nGetRunningObjectTable(0, &pROT)");
return E_FAIL;
}
// test enumerate before usage
pROT->EnumRunning(&pMonikersTable);
hr = pMonikersTable->Reset();
if(FAILED(hr))
{
TRACE("\nFailed pROT->Register(.. ");
}
while(pMonikersTable->Next(1, &pCurrentMoniker, NULL) == S_OK)
{
iTestCount++;
TRACE("\n");
}
/*
hr = StringCchPrintfW(wsz, NUMELMS(wsz), L"FilterGraph %08x pid %08x\0", (DWORD_PTR)pUnkGraph,
GetCurrentProcessId());
*/
TRACE("\nCreateItemMoniker using name ");
hr = CreateItemMoniker(L"!", wsz, &pMoniker);
if (SUCCEEDED(hr)) {
TRACE("\nRegister moniker ");
// Use the ROTFLAGS_REGISTRATIONKEEPSALIVE to ensure a strong reference
// to the object. Using this flag will cause the object to remain
// registered until it is explicitly revoked with the Revoke() method.
//
// Not using this flag means that if GraphEdit remotely connects
// to this graph and then GraphEdit exits, this object registration
// will be deleted, causing future attempts by GraphEdit to fail until
// this application is restarted or until the graph is registered again.
hr = pROT->Register(ROTFLAGS_REGISTRATIONKEEPSALIVE, pUnkGraph,
pMoniker, pdwRegister);
if(FAILED(hr))
{
TRACE("\nFailed pROT->Register(.. ");
}
pMoniker->Release();
}
pROT->Release();
THIS IS A DIFFERENT "FUNCTION"
HRESULT hr;
TRACE("\nBind to control "); // Get a BindCtx.
IBindCtx *pbc;
hr = CreateBindCtx(0, &pbc);
if(FAILED(hr))
{
TRACE("\nCreateBindCtx()", hr);
return false;
}
TRACE("\nGet running-object table.");
IRunningObjectTable *prot;
hr = pbc->GetRunningObjectTable(&prot);
if(FAILED(hr))
{
TRACE("\nCreateBindCtx()", hr);
prot->Release();
return false;
}
TRACE("\nGet enumeration interface.");
IEnumMoniker *pem; // enumerated moniker
hr = prot->EnumRunning(&pem);
if(FAILED(hr))
{
TRACE("\nCreateBindCtx()", hr);
prot->Release();
pbc->Release();
return false;
}
// Start at the beginning.
pem->Reset();
// Churn through enumeration.
ULONG fetched; // actuall number of items retrived , could ve set to NULL
IMoniker *pmon;
int n = 0;
while(pem->Next(1, &pmon, &fetched) == S_OK)
{
TRACE("\nGet moniker for %s Graph name ", strName); // Get DisplayName.
LPOLESTR pName;
pmon->GetDisplayName(pbc, NULL, &pName);
// Convert it to ASCII.
char szName[512];
WideCharToMultiByte(CP_ACP, 0, pName, -1, szName, 512, NULL,
NULL);
TRACE("\nCompare with Moniker name %s",szName); // Compare it against the name we got in SetHostNames().
if(strName.CompareNoCase(szName) /* "!Video")*/ == 0 )
//if(!strcmp(szName, "!Video")) //MN for now
{
TRACE("\nBind to this ROT entry moniker named %s",strName);
/*
IDispatch *pDisp;
hr = pmon->BindToObject(pbc, NULL, IID_IDispatch, (void
**)&pDisp);
*/
IUnknown *pUnkGraph;
hr = pmon->BindToObject(pbc, NULL, IID_IUnknown, (void
**)&pUnkGraph);
if(!FAILED(hr))
{
TRACE("\nBound to object ");
// Remember IDispatch.
// m_pDocDisp = pDisp;
// Notice...
//sprintf(buf, "Document IDispatch = %08lx",
//m_pDocDisp);
//DoMsg(buf);
break;
}
else
{
AfxMessageBox("Failed IUnknown *pUnkGraph;");
TRACE("\nFailed BindToObject() %i ", hr);
}
}
// Release interfaces.
pmon->Release();
// Break out if we obtained the IDispatch successfully.
// if(m_pDocDisp != NULL) break;
}
// Release interfaces.
pem->Release();
prot->Release();
pbc->Release();
<pre lang="text">PS I cannot do "preview" getting "specified module cannot be found".
So if tthis post is too messy, sorry about that </pre>
|
|
|
|
|
Why are you trying to put <pre> tags around your question and comments instead of round your code?
Use the best guess
|
|
|
|
|
Because this site does not do spell check and I do my editing elsewhere and then paste it in.
And I do not pay attention that the entry is by default “code” and than it gets too messy to edit.
Besides I cannot do preview also.
I have no clue why the HTML tags gets posed in the text.
So, thanks for the comments.
I guess the answer is – I am too lazy.
But what is this "plain text" selection for anyway?
Just pasted the above without selecting anything - so is this post nov a code???
Still cannot do preview.
Vaclav
|
|
|
|
|
An entry would normally be expected to take the following form:
- Initial details of the problem or question.
- Optional code snippets to help explain the problem. These should be surrounded by <pre> tags, and can be automated by selecting all the code with the mouse, and then using the code button above.
- Further text or code entries as appropriate
As an example:
This is the question, and the following code helps to explain it:
void foo(int number)
{
}
Further text follows ...
...
The preview should show below the edit window. If your code block does not show up formatted as above then ensure that the checkbox "Treat my content as plain text, not as HTML", below, is unchecked. Formatting your questions properly helps to ensure they get maximum visibility; some CP members ignore badly formatted entries.
Use the best guess
|
|
|
|
|
Hello All,
I am using GetLastInputInfo in my MFC app to find the idle time for the app, but in my testing i found that it's giving system wide idle time.
The reason i found that out is, i opened my app and let it stay on the background, and opened the outlook and started playing around with it. My app was supposed to be timed out after 1 min, but it didn't.
Then i stopped playing with outlook for a minute, and then my app timed out.
How to fix this?
|
|
|
|
|
My take on GetLastInputInfo is that it's per user session not per application. To do what you need? I'm not sure. Maybe create a thread that listens for user input events sent to you app, and sends notification to your app to close after the specified time?
|
|
|
|
|
Hi,
I am a beginner for Metro app development and trying to use the keydown event but not the keyup event with a text box element on the XAML. I have created two key event functions (up and down) from the text box, which are the below. The problem is the keydown function works only when the key is up but not down. If I hold the key press down, the event does not come out until the key up, which is the same as the keyup function.
I am working to get keyboard input from the touch softkeyboard. For example, after invoking the touch keyboard with the text box element, I like to play a unique sound only when a key (e.g., 'Q' or 'K') is pressed.
Anyone knows how to fix this problem? I also appreciate that you can help me know the alternative way with an example source code. Thank you a lot in advance.
MainPage::OnKeyUp(Platform::Object^ sender, Windows::UI::Xaml::Input::KeyRoutedEventArgs^ e)
{
if((int)e->Key == 112) // F1 key
{
errorText->Text = "F1 Key up";
}
}
MainPage::OnKeyDown(Platform::Object^ sender, Windows::UI::Xaml::Input::KeyRoutedEventArgs^ e)
{
if((int)e->Key == 112) // F1 key
{
errorText->Text = "F1 Key down";
}
}
|
|
|
|
|
Shouldn't this be in the c# forum?
Nevermind.
Make it work. Then do it better - Andrei Straut
|
|
|
|
|
You might have more luck with Microsoft's developer forums for your particular needs.
social.msdn.microsoft.com/Forums/en-US/categories
"It's true that hard work never killed anyone. But I figure, why take the chance." - Ronald Reagan
That's what machines are for.
Got a problem?
Sleep on it.
|
|
|
|
|
Hi guys,
here is my problem description:
I receive messages over a bus. Suppose there is a function to read the messages storing them into a struct defined as follows:
typedef struct
{
ULONG timestamp;
BYTE ID;
BYTE data_length;
BYTE data[8];
} MSG_FRAME;
Depending on the message ID different messages represent different values for one project.
For example msg with ID 10 can include in the 8 bytes something like:
width: 6 bits
height: 6 bits
actpos: 12 bits
maxpos: 12 bits
minpos: 12 bits
range: 16 bits
total: 64 bits = 8 bytes
Here comes the tricky part. I want to print out the specific information hidden in the 8 bytes. I can define the structures for every msg ID and compile the program with this "special" header file, but I want to do it during runtime of the program, loading the information regarding the msgs, because i can have different projects where the information for different msg IDs can differ.
I've a non-C file, where basically all the information is written. Lets stay frame named
GetStatus{
bit 0 - 7 width
bit 8 - 15 height
.
.
.
}
etc.
How to read it on runtime and decode the messages? On runtime I'm not able to create variables and structures anymore!
Any advices? Is some sort of scripting language the solution? Which, how?
Thanks in advance for all replays!
|
|
|
|
|
With that information it is just a matter of decoding the structure and processing each element of the message. in pseudocode form something like:
string title := structure_header
while NOT end_of_structure
int field_start := next_word // the starting field position
int field_end := next_word // the ending field position
int field_width := (field_end + 1) - field_start
string field_name := next_word // the field's name
end while
// add code to extract the data and print according to the above variables
Use the best guess
|
|
|
|
|
Hi Richard, thanks for your reply.
I've considered this option, but I'm afraid of computation time. Suppose you have to do this every time for every message you receive.
Step 1: Read the info about msg frames from pre-defined file and store it in some array - "Look-up table"
Step 2: On receive message use LOT and then read bit for bit to decode the message. - This step is in my opinion very time expensive for computation.
Sure, you can skip messages, when there is no change, but you have to check with previous result, which can add some additional computation.
Maybe I'm to careful, but I want to pick the right way, before coding myself to dead just to get an unsatisfactory result.
|
|
|
|
|
If you know all the message types and their structures then the best option would be to build those structures into your code. If you cannot do this, and have to rely on this data file, then you could always build your tables at the start of the program. In either case the only time overhead is the extraction of the different fields from the messages, and that is something you have to live with (unless you know how to change the laws of physics).
Use the best guess
|
|
|
|
|
You are right, I can prepare an *.h file where all the structs will be defined and compile the whole program with this h-file. The problem is, when I hava a different project, where the message names differ and even their content, then I had to create new h-file and compile the whole application again with this h-file.
If some other user wants to use this bus monitor for his own project, he won't be able, because he had to prepare this h-file first and then compile it.
So I need to "compile" this file at runtime and add it to the application somehow. Or this is my idea. How this is implemented in real I don't have a clue.
|
|
|
|
|
HellMaster[cz] wrote: How this is implemented in real I don't have a clue. I explained how in my earlier message. Read the file at the beginning of the program and create a table containing information about the different message types and their structures. You then use the appropriate entry in the table to decode each message as it arrives.
Use the best guess
|
|
|
|
|
Thanks for your effort Richard. Probably you are right. I'll be thinking about some different solution, but meanwhile I'll try to implement something you suggested.
Thank you very much for your time and advices!
|
|
|
|
|
Sorry to butt in, but you mentioned this is a bus monitor. I bought a CANbus, rather than LINbus, monitor (CANalyzer) where the whole point was that, yes, you had to write a script to decode and display the messages because as you say, they can be different depending on the application. Though I've seen other low end monitors that simply displayed the bits.
|
|
|
|
|
Hi,
Your fields remind me of dealing with things like CANbus messages, but then the possible structures were known before hand. Is there a common structure you could define that you could copy each instance of input data into putting bits in appropriate places and padding out, or marking fields as empty if not needed?
|
|
|
|
|
Hi Jonathan,
you are right. But insted of handling CANbus messages I'm handling LINbus messages. The frames are really known and are specified in a file called LIN description file *.ldf (the format of file is defined in the specification). But how do I create structs from this file at runtime to use it for decoding a message? Or what would be the best approach?
Basically I would be able to define some struct like:
struct
{
bit0 :1
bit1 :1
bit2 :1
bit3 :1
.
.
.
bit64 :1
}
and then assign all 8 bytes to this struct and decode it bit-by-bit. But seems to me to be still clumsy and slow. Or this is just the only one solution.
Maybe the solution is really clear and simple, but I'm missing something.
|
|
|
|
|
Hi,
I read LINbus is the cheaper alternatic to CANbus used by various car manufacturers, so I doubt it would get there without being easy to interface to.
You can design a system, module, program etc if you know what the messages and interactions are: it's inputs and outputs and the behaviour expected. If you don't know such things, you're in trouble: by that design rule anyway. So I'd check, are these messages really as randomly organized as you think? It could be there are only three of four possibilities, not ten or fifty.
Is there someone writing the code to send these messages that doesn't understand the LINbus spec? I was put off by someones CANbus coding when they said 'it's too complicated, you wouldn't understand'. Their code failed, I got a copy of the CANbus spec, read it, understood it, and we produced a great system.
You can't be the first to have done this. Get in touch with manufacturers of the kit you're using to try and het their demo code and base what you're doing on that. Much easier than starting from scratch.
Or try using Unions, pre-define them for the possible message formats then apply the correct one based on the ID.
|
|
|
|
|
Whatever you do you have to know the structure of every message. You can either create a load of structures in the code before you compile or you can write code that at run time pulls the data apart, which is more work.
If you really have an idf file that defines the data why not write a program that can read the idf file and produce a header file from it? That is if writing the header file yourself is too much.
==============================
Nothing to say.
|
|
|
|
|
Let's say I have a window created and I'm interested on certain region in that window (say rect(10,10,300,300)) and want to somehow crop it and save as an image file.
May I know how can I do this?
|
|
|
|
|
You can copy part of the Window to a new device context using the BitBlt function[^]. This article[^] shows how to save it to a file.
Use the best guess
|
|
|
|
|