|
MsmVc wrote: I try to run dos command through mfc vc++.
If you just want to run a command from you application, go fot the functions mentioned in earlier reply.
If you are aiming to run a command and show its output in a window in your application, you have to go for Pipe communication.
|
|
|
|
|
C WinAPI solution (you can adopt it in mfc easily i think):
#include <windows.h>
#include <stdio.h>
char * ExecuteCMD(char * command)
{
SECURITY_ATTRIBUTES sec;
PROCESS_INFORMATION pi;
STARTUPINFO si;
HANDLE hOutR, hOutW;
DWORD BTAvail;
char * Result = NULL;
char * cmdline = NULL;
char cmdpath[256];
OSVERSIONINFO OSVersionInfo;
DWORD Read = 0;
ZeroMemory(&pi, sizeof(PROCESS_INFORMATION));
ZeroMemory(&sec, sizeof(SECURITY_ATTRIBUTES));
sec.nLength = sizeof(SECURITY_ATTRIBUTES);
sec.bInheritHandle = TRUE;
sec.lpSecurityDescriptor = NULL;
if (CreatePipe(&hOutR, &hOutW, &sec, 0))
{
ZeroMemory(&si, sizeof(STARTUPINFO));
si.cb = sizeof(STARTUPINFO);
si.hStdOutput = hOutW;
si.dwFlags = STARTF_USESTDHANDLES|STARTF_USESHOWWINDOW;
cmdline = (char *) GlobalAlloc(GMEM_FIXED, (7 + lstrlen(command)));
lstrcat(lstrcpy(cmdline, " /a /c "), command);
OSVersionInfo.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
GetVersionEx (&OSVersionInfo);
if (OSVersionInfo.dwPlatformId == VER_PLATFORM_WIN32_NT)
{
GetEnvironmentVariable("ComSpec", cmdpath, 2048);
}
if (CreateProcess(cmdpath, cmdline, &sec, &sec, TRUE, NORMAL_PRIORITY_CLASS, NULL, NULL, &si, &pi))
{
WaitForSingleObject(pi.hProcess, INFINITE);
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
PeekNamedPipe (hOutR, NULL, 0, NULL, &BTAvail, NULL);
if (BTAvail > 0)
{
Result = (char *) GlobalAlloc(GMEM_FIXED, BTAvail + 1);
ReadFile(hOutR, Result, BTAvail, &Read, NULL);
Result[BTAvail] = '\0';
OemToChar(Result, Result);
return Result;
}
}
}
return NULL;
}
int main(int argc, char **argv)
{
char *cmd = ExecuteCMD("dir");
puts(cmd);
}
011011010110000101100011011010000110100101101110
0110010101110011
|
|
|
|
|
Thanks for solution
Problem solved
|
|
|
|
|
I've been thinking of adopting a new style of namespace use. Well, new to me because I've never seen it used this way anywhere else. The common thing to do in C++ is to have one class to be declared per header file. The only other things that would be declared in the header would be closely related functions and types (helper functions, friend classes, typedefs, enums, etc.). Often, the header file is named after the class.
I propose declaring each class inside it's own namespace. The namespace would be named after the class, except in all lowercase with words seperated by underscores. Classes would be named using the traditional PascalCasingScheme. Related classes and functions would go inside this namespace. Any public nested classes, typedefs, and enums would be at namespace scope instead of being declared inside the class.
For example, this is the before:
class FancyWidget
{
public:
typedef std::vector<std::string> WidgetIds;
typedef std::map<std::string, int> WidgetPrices;
enum WidgetColors
{
RED,
WHITE,
BLUE
};
class Exception : public std::exception
{
...
};
class InvalidWidgetException : public Exception
{
...
};
...
};
and this is the after:
namespace fancy_widget
{
typedef std::vector<std::string> WidgetIds;
typedef std::map<std::string, int> WidgetPrices;
enum WidgetColors
{
RED,
WHITE,
BLUE
};
class Exception : public std::exception
{
...
};
class InvalidWidgetException : public Exception
{
...
};
class FancyWidget
{
...
};
}
I tend to make things that are closely related to a class nested inside the class declaration, friend classes in particular. However, this practice always led to a convoluted class declaration. Using this style moves it out of the class declaration, but still conveys a strong association with that class through the namespace.
I can't think of any negatives to this approach, hence why I'm posting this to the forum. I don't think the extra typing whenever you needed to use anything inside "fancy_widget" would be a bother and if it did there's always the "using" keyword. Anybody see anything wrong with doing this?
This sort of thing seems to be somewhat common in Python (except they frown upon using underscores in package names). Which is why I've started thinking about carrying it over into my C++.
|
|
|
|
|
I don't think its an abuse, thats how namespaces have to be used.
typedef std::vector<std::string> WidgetIds;
typedef std::map<std::string, int> WidgetPrices;
Will be global variables in second approach, are you OK with that ?
|
|
|
|
|
Madhu Nair wrote: typedef std::vector<std::string> WidgetIds;
typedef std::map<std::string, int=""> WidgetPrices;
Will be global variables in second approach, are you OK with that ?
What do you mean? Those are typedef's for std::vector and std::map and would be used like so:
fancy_widget::WidgetIds foo;
fancy_widget::WidgetPrices bar;
|
|
|
|
|
Yes and no.
Yes, they are globally accessible, but that was also true in the first approach, since they've been declared public.
No they are not global, because, technically, only things declared outside a struct, class or namespace are considered global. These symbols are local to the newly defined namespace. It's about as close to global that you can get though, since - similar to true global definitions - the declaration s of the elements of a namespaces may be spread out over the codebase, so you're not guaranteed to see everything that's in a namespace when you look at a namespace declaration.
This can be considered a disadvantage, but it is also an advantage, as you can later extend a namespace for internal use without having to change a header and thus the interface of whatever you're trying to encapsulate. You might also deliberatly spread different parts of the namespace over several headers in order to minimize dependencies and thus reduce compile times.
|
|
|
|
|
Hi,
asincero wrote: I can't think of any negatives to this approach
The only drawback is that a class can be used as template argument and a namespace cannot. If not relevant in your case, the namespace approach is indeeed cleaner.
cheers,
AR
When the wise (person) points at the moon the fool looks at the finger (Chinese proverb)
|
|
|
|
|
A related question I have regarding the use of namespaces in C++ (assuming Visual Studio) is in the past, I've found using the "ClassWizards" (or whatever they call the current feature) put any new methods outside of the namespace curly brackets requiring me to move new methods into the namepace.
Did they finally fix that and if so what version of Visual Studio are you using that places new methods correctly in their namespace?
thanks.
|
|
|
|
|
Hi,
How can I change the image attribute using gdi+?
I need to change the image size using gdi+.
|
|
|
|
|
|
Hi John,
You can use DrawImage() to rescale images...
Regards,
Spk521
|
|
|
|
|
Image::GetThumbnailImage and Bitmap::SetResolution (see eg. in earlier reply) method are apt for this.
DrawImage() method simply draws the image with given dimension, it doesn't provide option to alter the source image, or to retrieve resized image.
|
|
|
|
|
Im using VS2008.
Im sending data to LED in external device thorugh serial port by given in set of format for glowing LED.
But it show error on Release() in nRef!=0 line.
Actually it works fine in VC6.0Now im converting that to VS2008.
struct SLEDFrame
{
char *idtfr,*Data1,*Data2;
};
SLEDFrame btnLED[] = {
"0x53", "0x30", "0x30",
"0x53", "0x30", "0x31",
"0x53", "0x30", "0x32",
"0x53", "0x30", "0x33",
"0x53", "0x30", "0x34",
};
void CMSCOMMDlg::setLED(int pLEDNo, BOOL pState)
{
BYTE bID, bByte1, bByte2, bState, bCheckSumByte1, bCheckSumByte2;
if (pState)
bState = 0x31;
else
bState = 0x30;
CString tempStr, C1, C2;
tempStr = btnLED[pLEDNo-1].idtfr;
tempStr = tempStr.Mid(2);
sscanf(tempStr, "%x", &bID);
tempStr = btnLED[pLEDNo-1].Data1;
tempStr = tempStr.Mid(2);
sscanf(tempStr, "%x", &bByte1);
tempStr = btnLED[pLEDNo-1].Data2;
tempStr = tempStr.Mid(2);
sscanf(tempStr, "%x", &bByte2);
BYTE bSum = bByte1 + bByte2 + bState;
tempStr.Format("%x",bSum);
C1 = "0x3" + tempStr.Left(1);
C2 = "0x3" + tempStr.Right(1);
sscanf(C1, "%x", &bCheckSumByte1);
sscanf(C2, "%x", &bCheckSumByte2);
CByteArray btArray;
btArray.Add (bID);
btArray.Add (bByte1);
btArray.Add (bByte2);
btArray.Add (bState);
btArray.Add (bCheckSumByte1);
btArray.Add (bCheckSumByte2);
COleVariant var(btArray);
m_Comm.SetOutput(var);
}
The error occurs in
void Release() throw()
{
ATLASSERT( nRefs != 0 );
if( _AtlInterlockedDecrement( &nRefs ) <= 0 )
{
pStringMgr->Free( this );
}
}
Anu
|
|
|
|
|
Why are you using strings? This is slow and pointless.
You should read up on bitwise operations[^] such as shift and AND
struct SLEDFrame {
BYTE idtfr;
BYTE Data1;
BYTE Data2;
};
SLEDFrame btnLED[] = {
{ 0x53, 0x30, 0x30 },
{ 0x53, 0x30, 0x31 },
{ 0x53, 0x30, 0x32 },
{ 0x53, 0x30, 0x33 },
{ 0x53, 0x30, 0x34 },
{ 0x53, 0x30, 0x35 },
}
void CMSCOMMDlg::setLED(int pLEDNo, BOOL pState) {
BYTE bState;
if (pState) {
bState = 0x31;
} else {
bState = 0x30;
}
BYTE bID = btnLED[pLEDNo-1].idtfr;
BYTE bByte1 = btnLED[pLEDNo-1].Data1;
BYTE bByte2 = btnLED[pLEDNo-1].Data2;
BYTE bSum = bByte1 + bByte2 + bState;
BYTE bCheckSumByte1 = 0x30 | (bSum >> 4);
BYTE bCheckSumByte2 = 0x30 | (bSum & 0x0F);
CByteArray btArray;
btArray.Add(bID);
btArray.Add(bByte1);
btArray.Add(bByte2);
btArray.Add(bState);
btArray.Add(bCheckSumByte1);
btArray.Add(bCheckSumByte2);
COleVariant var(btArray);
m_Comm.SetOutput(var);
}
|
|
|
|
|
Can you tell me how can i convert CString to BYTE.
I used
CString C2 = "0x30h"
BYTE bCheckSumByte2 ;
bCheckSumByte2 = (BYTE)(LPCSTR)C2;
But it doesnt work.
Anu
|
|
|
|
|
I would recommend using strtol[^]
CString strNumber = "0x30h";
BYTE nNumber = (BYTE)strtol(((LPCSTR)strNumber) + 2, NULL, 16);
The +2 is to skip over the 2 characters "0x"
The 16 is the base (hexadecimal)
The function returns a long, we need to cast this down to a BYTE
|
|
|
|
|
You cannot convert from string to binary by the use of casts. Casts are used to tell the compiler to treat a variable of type X as type Y; however, no conversion of the variable's content will be done.
I must get a clever new signature for 2011.
|
|
|
|
|
Hi,
I am working on VS 2010, I am getting this error, which I didn't able to solve it inspite of adding the linking file(.lib) in the Project Properties -> Link -> Additional Libraries. I am not getting the error in VS6.
what is the reason for this error? I have searched other forums, one of the suggestion I got is it is bug in VS, you need to rename the file, remove it from the project and add it back. Even though I am not satisfied with the suggestion, I tried it, since I have no other option. But it proved invain, I am getting the same error with the new file as well.
Thanks in advance
Charan
|
|
|
|
|
Linker errors arise due to a mismatch in the function declaration/definition. This is because the linker cannot find the exact definition of the function you are trying to use in your code while linking.
You did'nt mention what linker error you are getting. Is it related to some function that you have written or a function that is in a third party library and you are trying to use it?
Please provide more details.
I am a HUMAN. I have that keyword in my name........
_AnsHUMAN_
|
|
|
|
|
Thanks for the reply, I am getting the error for the same function I have written in the lib.
I am trying to link MFC shared dll to static library, I am getting unresolved error while I am compiling shared dll.
|
|
|
|
|
What is the actual symbol that is unresolved? Also show us the definition of the symbol that is unresolved from the .h or .cpp file (you don't need to show us the code, it makes no difference)
You can try a full rebuild of the project, occasionally the compiler gets a little confused.
Also, it is best to use a library that is compiled with the same version of VS that you are linking from.
|
|
|
|
|
There is one more difference in VC6.0 and VS2010 is that library included in VC6.0 is space separated and VS2010 is semi colon separated. Tried it once.
I believe in LOVE AT FIRST SIGHT...
Bcoz I have loved my Mother...
even since I opened my eyes...(ICAN)
|
|
|
|
|
Yes it is right, I converted the project file using VS10, ';' is added automatically by VS10, I didn't have to worry about it.
Sorry for the late replay, I am using 2 static libraries(say A, B) one is dependent on other. Then I am linking MFC shared dll to these 2 static libraries. The error is, it is not finding the function in B which is called from A, while compiling the shared dll.
Thanks,
Charan
|
|
|
|
|
Hi guys,
Sorry If I am confusing you.
I want tobe more clear what I am saying
I have two static libraries A and B (where A is dependent on B)
Now I am compiling dynamic library, which depends on both A and B.
I am getting the error:
simwfp.lib(SimUserErrDlg.obj) : error LNK2001: unresolved external symbol "public: __thiscall CSimString::CSimString(unsigned short const *)" (??0CSimString@@QAE@PBG@Z)
Thanks,
Charan
|
|
|
|
|