|
|
Try System.Diagnostics.Stopwatch
David Anton
Convert between VB, C#, C++, & Java
www.tangiblesoftwaresolutions.com
|
|
|
|
|
|
Timer () only to 1 Ms fastest, write their own a Delay ().
|
|
|
|
|
I'm still new to programming in Windows, so don't kill me if the answer is obvious.
What I've attempted to do is write my own WinMain function. I've based it off of Visual's generated code with a little Programming Windows mixed in combined with my own code.
Well it doesn't work, but I can't spot why.
#include <Windows.h>
#include <strsafe.h>
ATOM RegisterMyClass(HINSTANCE, wchar_t*);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void ErrorExit(LPTSTR lpszFunction);
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hpInst, LPSTR cmd, int nCmdShow)
{
HWND hwnd;
MSG msg;
wchar_t myWndClass[] = L"AWindow";
wchar_t myWndTitle[] = L"My Window";
RegisterMyClass(hInst, myWndClass);
hwnd = CreateWindowW(myWndClass, myWndTitle, WS_OVERLAPPEDWINDOW,
10, 10, 300, 300, NULL, NULL, hInst, NULL);
if ( hwnd == NULL )
ErrorExit(L"CreateWindow");
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
while ( GetMessage (&msg, NULL, 0, 0 ) )
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
ATOM RegisterMyClass(HINSTANCE hInst, wchar_t* wndClass)
{
WNDCLASSEX w;
w.cbSize = sizeof(WNDCLASSEX);
w.style = CS_HREDRAW | CS_VREDRAW;
w.lpfnWndProc = WndProc;
w.cbClsExtra = 0;
w.cbWndExtra = 0;
w.hInstance = hInst;
w.hIcon = LoadIcon(NULL, IDI_APPLICATION);
w.hCursor = LoadCursor(NULL, IDC_ARROW);
w.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
w.lpszMenuName = NULL;
w.lpszClassName = wndClass;
w.hIconSm = NULL;
return RegisterClassEx(&w);
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp)
{
HDC hdc;
PAINTSTRUCT ps;
switch (msg)
{
case WM_CREATE:
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
DefWindowProc(hWnd, msg, wp, lp);
}
return 0;
}
void ErrorExit(LPTSTR lpszFunction)
{
LPVOID lpMsgBuf;
LPVOID lpDisplayBuf;
DWORD dw = GetLastError();
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dw,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,
0, NULL );
lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,
(lstrlen((LPCTSTR)lpMsgBuf) + lstrlen((LPCTSTR)lpszFunction) + 40) * sizeof(TCHAR));
StringCchPrintf((LPTSTR)lpDisplayBuf,
LocalSize(lpDisplayBuf) / sizeof(TCHAR),
TEXT("%s failed with error %d: %s"),
lpszFunction, dw, lpMsgBuf);
MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK);
LocalFree(lpMsgBuf);
LocalFree(lpDisplayBuf);
ExitProcess(dw);
}
Using Microsofts own ErrorExit code, I get the following message when CreateWindow returns NULL:
CreateWindow failed with error 0: the operation completed successfully
What confuses me is that the MSDN documentation on CreateWindow() says that if the function fails, it returns NULL and to use GetLastError to find out what it was. Well, it returns a null, but then reports that there is no error.
I'm seriously boggled by this one.
|
|
|
|
|
Ah, so close....You've got a very subtle error in your WndProc.
Change your default handling to this:
default:
return DefWindowProc(hWnd, msg, wp, lp);
Your code was dropping the return code of DefWindowProc on the floor and returning zero which is bad news.
Returning zero from WM_NCCREATE instructs CreateWindow to stop the window creation. DefWindowProc will return 1 instead of 0 and allow the creation to proceed.
John
|
|
|
|
|
I knew it had to be something incredibly simple *smacks palm to forehead*. Thanks a bunch.
|
|
|
|
|
hi!
Help me to read data from Comport RS232:
My function to sent data to RS232:
private: public: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
Random^ gen=gcnew Random;
serialPort1->Write(Convert::ToString(gen->Next(0,100)+"P"));
}
and my function to read data from RS232
public: System::Void serialPort1_DataReceived(System::Object^ sender, System::IO::Ports::SerialDataReceivedEventArgs^ e) {
String^ st = serialPort1->ReadExisting();
button1->Text=st;
}
i can buid solution.After that i conect pin 2 and pin 3 of COMPORT.Next when run program had error nowbutton1->text=st;
|
|
|
|
|
Hi,
most if not all asynchronous events are handled by threads from the ThreadPool, not by the main thread, which means you can't touch GUI Controls right away from those handlers. You need to use InvokeRequired/Invoke, as I explain in this article[^] of mine (with C# and VB.NET examples, no C++/CLI, sorry, although the principles are exactly the same).
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
My friend, your code is written with what IDE? I have a VC6.0 example, but I don't know how to upload, you leave the email address, I send you a past
|
|
|
|
|
Thanks you LiangGuangLin ! my email: bqhoang89@gmail.com
|
|
|
|
|
Hi.
I want to statically link to a DLL, 'USBm.dll'(From USBMicro.com, a Hardware Interface that allows one to interface Relays etc. via a USB device they sell.)
I use VC5.00, so I require a Lib file to link with. My Compiler generates from a C-function named foo() the 'Decorated' name: '_foo' (it adds a leading Underscore). Unfortunately, the functions in the DLL are exported without such leading underscore. (The DLL has Borland Compiler suggestions in the way it was put together, I suspect that that's the compiler they (who wrote the DLL)use)
Normally I would create a DLL project with 'DoNothing' functions for each of the functions in the DLL Header. Discard the DLL, but, keep the Lib.
How do I convince My VC 5.00 compiler to emit obj files without decorating the function name. Is there an Obscure Command Line Switch
Regards,
Bram van Kampen
|
|
|
|
|
Hi Bram,
this sounds like a native C/C++ question, not a managed C++/CLI question, hence IMO you're in the wrong forum.
If your code is native C, I know of three ways to avoid C++ identifier decoration:
1. use the .c extension; that should be enough to tell the compiler it is not C++ code, it works for Visual Studio.
2. use some compiler switch, depends on the specific tool suite.
3. use the extern "C" keyword thingies, as in:
extern "C" {
}
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
Luc Pattyn wrote: this sounds like a native C/C++ question, not a managed C++/CLI question, hence
IMO you're in the wrong forum.
Well, the forum for C/CPP seems to have disappeared, to be replaced by a Question/Answer Message Board
Luc Pattyn wrote: extern "C" {
}
This Still decorates the names with a leading underscore
Bram van Kampen
|
|
|
|
|
Bram van Kampen wrote: C/CPP seems to have disappeared
I see it in the alphabetical list at the left, between ATL and C#.
Bram van Kampen wrote: This Still decorates the names...
It shouldn't. Anyway, I gave you three ways. If none of them work for you, I can only suggest you read the manual that came with your IDE.
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
Hi,
I just found a C# example for creating a word document using OpenXML format which I've linked below. Can you please help me write the same in C++/CLI or suggest me any links where I can find c++\cli openxml sdk creating a word doc examples or any tutorial related to this.
Creation of a Word 2007 document using the Open XML Format SDK[^]
thanks an advance
|
|
|
|
|
"OpenXML" is the format of the document.
That doesn't have anything to do with C++ nor C#.
Rather you write code to produce that. In either C++ or C#.
The basic format is XML. So any C++ library that allows you to output XML would assist you in writing XML allows you to do that.
Actually for the tutorial link given you do not even need a library. One can use simple output statements.
And that should be sufficient if you understand how to program in C++ and have a basic understanding of XML and are willing to learn the standard for OpenXML.
If not then you have a different problem. You certainly can't do anything if you do not have a basic grasp of C++. To use OpenXML you do need a basic understanding of XML and if you do not have it then attempting OpenXML is NOT the way to start. Additionally that is not the only way to create a Word doc if that is your actual requirement.
|
|
|
|
|
Hi,
I have launched a process (another exe) from my application using the following:
CreateProcess(NULL,pCmdLine,NULL,NULL,TRUE,CREATE_NEW_CONSOLE,NULL,NULL,&si,&pi);
So now the requirement is that to pass different pCmdLine to the same process, if it's already running.
So how can I do this in VC++?
Please provide me some sample code.
|
|
|
|
|
If it's a console app then you can only pass parameters to it on invocation, i.e. in your CreateProcess() call. If it's a Windows app then you can use SendMessage() to send private messages to it.
The best things in life are not things.
|
|
|
|
|
Hi,
I have one linux cpp executable file. i want how to run this executable file in windows OS?.
please give any idea? or urls
Regards,
M.Mathivanan
|
|
|
|
|
Hi,
1.
you're in the wrong forum, C++ on Linux most likely will be "native C++", not be "managed .NET code".
2.
it has been many years ago I last did this, back then "cygwin" was the answer. Try Googling it.
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
If you have Windows 7 Ultimate or Enterprise, you can install the 'Subsystem for Unix-based Applications' (SUA) which you get a Windows compatible gcc compiler with g++ and all the Unix/Linux goodies. You will have to re-compile your C++ file in this new Posix environment for it to run in Windows.
For into how to install go to: http://www.suacommunity.com/SUA.aspx[^]
|
|
|
|
|
mathivanaan wrote: please give any idea?
1. Recode it so it is a windows code, and then recompile. This could be a lot of work or little work depending on what the executable does.
2. Find/write a simulator/virtual machine that 'runs' an execuble from the targeted system on windows.
3. Create a server that runs on a linux box. It runs the executable. It has a communications API. Your Windows app talks to that server.
In the above 2 is probably the most difficult solution by far.
|
|
|
|
|
Hi, I'm traying to use Berkeley DB in C++/CLI with /clr mode. I wrote two applications, one will store to a database and another one will try to read the contents of that database, but i failed in doing that!
This the first code (first app) for writing to a database:
#include "stdafx.h"
#pragma comment(lib,"libdb51")
using namespace System;
using namespace System::Runtime::InteropServices;
int main(array<System::String ^> ^args)
{
Db SigDb(0,0);
unsigned int oFlags= DB_CREATE;
SigDb.open(NULL,"SigDb.db",0,DB_BTREE,oFlags,0);
String^ HexSig="1E81F1C1176434";
wchar_t* a=( wchar_t* )Marshal::StringToHGlobalUni(HexSig).ToPointer() ;
wchar_t* A=( wchar_t* )Marshal::StringToHGlobalUni(HexSig).ToPointer();;
Dbt key1(a,100);
Dbt data1(A,100);
int ret= SigDb.put(NULL,&key1,&data1, DB_NOOVERWRITE);
if(ret==DB_KEYEXIST){
Console::WriteLine("You are trying to insert an exist key!");
}
SigDb.close(0);
Marshal::FreeHGlobal(IntPtr(A));
Marshal::FreeHGlobal(IntPtr(a));
return 0;
}
and this is the second code for reading from the database:
#include "stdafx.h"
#pragma comment(lib,"libdb51")
using namespace System;
using namespace System::Runtime::InteropServices;
int main(array<System::String ^> ^args)
{
Db SigDb(0,0);
unsigned int oFlags= DB_CREATE;
SigDb.open(NULL,"SigDb.db",0,DB_BTREE,oFlags,0);
String^ HexSig="1E81F1C1176434";
wchar_t* a=( wchar_t* )Marshal::StringToHGlobalUni(HexSig).ToPointer();
SigDb.open(NULL,"SigDb.db",0,DB_BTREE,oFlags,0);
wchar_t DDData[200];
Dbt getKey, getData;
getKey.set_data(a);
getKey.set_size(100);
getData.set_data(DDData);
getData.set_ulen(200);
getData.set_flags(DB_DBT_USERMEM);
if(SigDb.get(NULL,&getKey,&getData,0)==DB_NOTFOUND)
Console::WriteLine("Not Found !");
else
Console::WriteLine(" {0}",Marshal::PtrToStringUni((IntPtr)DDData));
Marshal::FreeHGlobal(IntPtr(a));
Console::ReadLine();
return 0;
}
always the second app says "Not found"! On the other hand when i tried to write the same data twise in the database using the first code, it says "You are trying to insert an exist key!" and that proves that the data was written! Any idea pls!
|
|
|
|
|
As a guess if the data type in the database is char (and not varchar) then normally databases fill the column.
Your usage suggests a column width of 100 but a value that is less than that. A fill character, in my experience, is space. So for your query you must explicitly space fill it yourself.
|
|
|
|
|