|
Can use ShellExecuteEx / ShellExecute. Have a look @ MSDN
|
|
|
|
|
Thanks, this works great!
|
|
|
|
|
Hi all,
I am relatively new to api programming.I am trying to automate a process and am using EnumWindows to get all window handles. I am then using GetWindowText and strcmp to check which of the window handles corresponds to the relevent window. However it doesnt seem to work i.e., it doesnt simulate the push of the button....
I have pasted the code snippet below.. Any suggestions on how to resolve this will be terrific.
In the main program i have
EnumWindows((WNDENUMPROC) WinCallback, 0);
My function is:
BOOL CALLBACK WinCallback(HWND win, LPARAM nadda)
{
char temp[512];
GetWindowText(win, temp, 512);
printf("%s",temp);
if(!strcmp(temp,"RBNal_Optocomm"))
{
HWND hcomm = FindWindowEx((HWND) win,NULL,"BUTTON","COM 1");
LRESULT lResult2;
lResult2 = SendMessage((HWND) hcomm,(UINT) BM_CLICK,0,0);
return FALSE;
}
}
Please let me know of any suggestions.
D
|
|
|
|
|
BM_CLICK message will fail if the window that will recieve the is not active. Try the code below.
if(!strcmp(temp,"RBNal_Optocomm"))
{
HWND hcomm = ::FindWindowEx((HWND) win,NULL,"BUTTON","COM 1");
if (hcomm)
{
::SetActiveWindow(hcomm);
LRESULT lResult2;
lResult2 = ::SendMessage((HWND) hcomm,(UINT) BM_CLICK,0,0);
return FALSE;
}
}
|
|
|
|
|
Thanks.. It still isnt working though.. I have passed an LPARAM value of 0 into the function.. Would this matter??
D
|
|
|
|
|
Hi,
try calling SetActiveWindow(win) before the line SetActiveWindow(hcoom). It should look like this
if (hcomm)
{
::SetActiveWindow(win);
::SetActiveWindow(hcomm);
LRESULT lResult2;
lResult2 = ::SendMessage((HWND) hcomm,(UINT) BM_CLICK,0,0);
return FALSE;
}
I had tested this and i had sent a sample applications (sender and receiver) in your email.
|
|
|
|
|
Hi
Thanks.. it worked....
Deepak
|
|
|
|
|
Hi all,
here is some interesting problem.... the problem setup goes like this...
a plane paper is placed on a table and a sphere is placed over it at some height and on that sphere at certain height a light was placed.
Noow as we all know it produces a shadow on the paper which will be some what elliptical if the light is not on the center line of the sphere. In this condition if we assume the paper as a 2D image then how can we calculate the pixels tht are covered under the elliptical shadow ( or to put it in other way while scannin form top how can we decide whether a pixel falls under the shadow).
any ideas ot thoughts are appreciated.
thanks in advance,
Suman
|
|
|
|
|
Google ray tracing.
Or DirectX will do it for you, if that's all you need.
Christian
I have drunk the cool-aid and found it wan and bitter. - Chris Maunder
|
|
|
|
|
the shadow is the intersection of the plane of the paper and the cone whose apex is at the light and whose sides are tangent to the sphere.
The distance from apex (light) to center of sphere is the hypotenuse of a triangle whose side opposite the apex angle is the radius of the sphere. This is enough to determine the cone.
|
|
|
|
|
Hi Scott,
yep u r right....i understood ur point. But the problem is how to tell whether a pixel lies in the shadow or not. I mean r there any techniques or approach to find it out.
becos i was given a sphere of some radius and the height at which it is and the pixel at which the center of the sphere lies. and i have to figure out all the pixels covering under its shadow...and then i have to do some other calcualtins for those pixels... but first of all i have to find those pixels.
hope i am clear to u.
thanks in advance,
Suman
|
|
|
|
|
Dear sirs:
I am learning VC++ and am having trouble compiling my project. The project includes several classes that inherit properties from other classes. I ran into just 3 "unresolved external symbol errors - LNK2019".
I am using the Visual Studio .NET 2003 platform to develop the application with. I have included the source files with this post and you can recreate the project by creating a new Win32 Console Application project named "use_student", and simply add the files into your project.
Any help would be much appreciated!
Thank you,
Rob Hyland
NOTE: if you just see a line of code that says "#include" all by itself it should read
"#include (bracket symbol) iostream (bracket symbol)"
***************************************************************************************************
*********************************** Header Files **************************************************
//
//////////////////////////////////////////////////////////////
// Listing 13.3 studentc.h. /
// studentc.h -- defining a Student class using containment /
//////////////////////////////////////////////////////////////
//
//
#ifndef _STUDNTC_H_
#define _STUDNTC_H_
#include <iostream>
#include "arraydb.h" // -- ArrayDB objects.
#include "strng2.h" // -- String objects (from Chapter 11).
using namespace std;
class Student
{
private:
String name;
ArrayDb scores;
public:
Student() : name("Null Student"), scores() {} // -- Use initialization syntax to init. objects.
Student(const String & s) : name(s), scores() {}
Student(int n) : name("Nully"), scores(n) {}
Student(const String & s, int n) : name(s), scores(n) {}
Student(const String & s, const ArrayDb & a) : name(s), scores(a) {}
Student(const char * str, const double * pd, int n) : name(str), scores(pd, n) {}
~Student() {}
double & operator[](int i);
const double & operator[](int i) const;
double Average() const;
// friends
friend ostream & operator<<(ostream & os, const Student & stu);
friend istream & operator>>(istream & is, Student & stu);
};
#endif
//
/////////////////////////////////////////
// strng1.h -- string class definition /
/////////////////////////////////////////
//
//
#include <iostream>
using namespace std;
#ifndef _STRNG1_H_
#define _STRNG1_H_
class String
{
private:
char * str; // pointer to string
int len; // length of string
public:
String(const char * s); // constructor
String(); // default constructor
String(const String & st);
//~String(); // destructor
int length() const { return len; } // -- Return the length of a stored string.
// overloaded operators
String & operator=(const String & st); // Assignment operator
String & operator=(const char * s); // Assignment operator #2
// friend functions
friend bool operator>(const String &st1, const String &st2);
friend bool operator<(const String &st, const String &st2);
friend bool operator==(const String &st, const String &st2);
friend ostream & operator<<(ostream & os, const String & st);
friend istream & operator>>(istream & is, String & st);
};
#endif
//
/////////////////////////////
// Listing 13.1 arraydb.h. /
// arraydb.h -- array class /
/////////////////////////////
//
//
#ifndef _ARRAYDB_H_
#define _ARRAYDB_H_
#include <iostream>
using namespace std;
class ArrayDb
{
private:
unsigned int size; // -- Number of array elements.
double * arr; // -- Address of first element.
public:
ArrayDb(); // -- Default constructor.
// -- Explicit keyword used to catch assignment errors (i.e. incorrect number of arguments).
explicit ArrayDb(unsigned int n, double val = 0.0); // -- Create an ArrayDb of n elements, set each to val.
ArrayDb(const double * pn, unsigned int n); // -- Create an ArrayDb of n elements, initialize to array pn.
ArrayDb(const ArrayDb & a); // -- Copy constructor.
virtual ~ArrayDb(); // -- Destructor.
// -- Returns array size.
unsigned int ArSize() const
{
return size;
}
double Average() const; // -- Return array average.
//
//////////////////////////
// Overloaded operators. /
//////////////////////////
//
virtual double & operator[](int i); // -- Array indexing; allow modifications.
virtual const double & operator[](int i) const; // -- Array indexing; read-only.
ArrayDb & operator=(const ArrayDb & a); // -- Assignment.
friend ostream & operator<<(ostream & os, const ArrayDb & a);
};
#endif
***************************************************************************************************
************************************** Source Files ***********************************************
***************************************************************************************************
//
//////////////////////////////////////////////////////
// Listing 13.4 studentc.cpp. /
// studentc.cpp -- Student class using containment /
//////////////////////////////////////////////////////
//
//
#include "studentc.h"
double Student::Average() const
{
return scores.Average(); // use ArrayDb::Average()
}
double & Student::operator[](int i)
{
return scores[i]; // use ArrayDb::operator[]()
}
const double & Student::operator[](int i) const
{
return scores[i];
}
// friends
// use String and ArrayDb versions
ostream & operator<<(ostream & os, const Student & stu)
{
os << "Scores for " << stu.name << ":\n";
os << stu.scores;
return os;
}
// use String version
istream & operator>>(istream & is, Student & stu)
{
is >> stu.name;
return is;
}
//
///////////////////////////////////////////
// Listing 13.2 arraydb.cpp. /
// arraydb.cpp -- ArrayDb class methods /
///////////////////////////////////////////
//
//
#include <iostream>
#include "arraydb.h"
using namespace std;
#include (bracket symbol) cstdlib (bracket symbol) // exit() prototype
//
//
/////////////////////////////////////////
// Default constructor -- no arguments. /
/////////////////////////////////////////
//
//
ArrayDb::ArrayDb()
{
arr = NULL; // -- Empty.
size = 0; // -- No value.
}
//
//
/////////////////////////////////////////////////////
// Constructs array of n elements, each set to val. /
/////////////////////////////////////////////////////
//
//
ArrayDb::ArrayDb(unsigned int n, double val)
{
arr = new double[n]; // -- Create new array.
size = n; // -- Number of elements in array.
// -- Loop through array.
for (int i = 0; i < size; i++)
arr[i] = val; // -- Place variable in array.
}
//
//
////////////////////////////////////////////////////
// Initialize ArrayDb object to a non-class array. /
////////////////////////////////////////////////////
//
//
ArrayDb::ArrayDb(const double *pn, unsigned int n)
{
arr = new double[n]; // -- Create new array.
size = n; // -- Number of elements in array.
// -- Loop through array.
for (int i = 0; i < size; i++)
arr[i] = pn[i]; // -- Assign value from one array to another.
}
//
//
/////////////////////////////////////////////////////////
// Initialize ArrayDb object to another ArrayDb object. /
/////////////////////////////////////////////////////////
//
//
ArrayDb::ArrayDb(const ArrayDb & a)
{
size = a.size; // -- Size of array dictated by ArrayDb object.
arr = new double[size]; // -- Create new array.
// -- Loop through array.
for (int i = 0; i < size; i++)
arr[i] = a.arr[i]; // -- Assign value from object-array to array.
}
//
//
///////////////////
// Deconstructor. /
///////////////////
//
//
ArrayDb::~ArrayDb()
{
delete [] arr; // -- Free-up memory created with new.
}
//
//
////////////////////////////
// Take average of grades. /
////////////////////////////
//
//
double ArrayDb::Average() const
{
double sum = 0; // -- Initialize sum-of-grades variable.
int i; // -- Counter variable.
int lim = ArSize(); // -- Get array size.
// -- Loop through array.
for (i = 0; i < lim; i++)
sum += arr[i]; // -- Summation of grades.
// -- If counter is true, then take average.
if (i > 0)
return sum / i;
// -- Else there were no grades to average.
else
{
cerr << "No entries in score array\n";
return 0;
}
}
//
//
////////////////////////////////////////////////////////////
// Let user access elements by index (assignment allowed). /
////////////////////////////////////////////////////////////
//
//
double & ArrayDb::operator[](int i)
{
// -- Check index before continuing.
if (i < 0 || i >= size)
{
cerr << "Error in array limits: "
<< i << " is a bad index\n";
exit(1);
}
return arr[i]; // -- Return with array-value.
}
//
//
///////////////////////////////////////////////////////////////
// Let user access elements by index (assignment disallowed). /
///////////////////////////////////////////////////////////////
//
//
const double & ArrayDb::operator[](int i) const
{
// --Check index before continuing.
if (i < 0 || i >= size)
{
cerr << "Error in array limits: "
<< i << " is a bad index\n";
exit(1);
}
return arr[i]; // -- Return with array-value (read-only).
}
//
//
/////////////////////////////
// Define class assignment. /
/////////////////////////////
//
//
ArrayDb & ArrayDb::operator=(const ArrayDb & a)
{
// -- Check to see if the object is assigned to itself.
if (this == &a)
return *this; // -- If it is, don't change anything.
delete [] arr; // -- Free-up memory.
size = a.size; // -- Get new size from object.
arr = new double[size]; // -- Create new array.
// -- Loop through array.
for (int i = 0; i < size; i++)
arr[i] = a.arr[i]; // -- Assign value from object-array to array.
return *this;
}
//
//
//////////////////////////////////////
// Quick output, 5 values to a line. /
//////////////////////////////////////
//
//
ostream & operator<<(ostream & os, const ArrayDb & a)
{
int i;
for (i = 0; i < a.size; i++)
{
os << a.arr[i] << " ";
if (i % 5 == 4)
os << "\n";
}
if (i % 5 != 0)
os << "\n";
return os;
}
//
///////////////////////////////////////////////////////////////////////////
// use_student.cpp : Defines the entry point for the console application. /
///////////////////////////////////////////////////////////////////////////
//
//
#include <iostream>
#include "stdafx.h"
#include "studentc.h"
using namespace std;
//
//
/////////////////////////
// Function prototypes. /
/////////////////////////
//
//
void set(Student & sa, int n);
//
//
///////////////////////////
// Variable declarations. /
///////////////////////////
//
//
const int pupils = 3;
const int quizzes = 5;
//
//
///////////////////////////
// Start of main program. /
///////////////////////////
//
//
int _tmain(int argc, _TCHAR* argv[])
{
Student ada[pupils] = {quizzes, quizzes, quizzes}; // -- Create student object arrays.
int i;
for (i = 0; i < pupils; i++)
set(ada[i], quizzes);
for (i = 0; i < pupils; i++)
{
cout << "\n" << ada[i];
cout << "average: " << ada[i].Average() << "\n";
}
return 0;
}
//
//
/////////////////////////////////////////
// Function to register pupil's scores. /
/////////////////////////////////////////
//
//
void set(Student & sa, int n)
{
cout << "Please enter the student’s name: ";
cin >> sa;
cout << "Please enter " << n << " quiz scores:\n";
for (int i = 0; i < n; i++)
cin >> sa[i];
while (cin.get() != '\n')
continue;
}
***************************************************************************************************
****************************** End of Source Files ************************************************
***************************************************************************************************
|
|
|
|
|
Hi,
i wrote a program in vc++ and it generates some data values which i am writing it into a .txt file. Now for ploting a graph for tht data I am importing this file in excel and plotting the graph. But what i want is to open an excel sheet directly from my proram while executing and write the data whenever generated into it or to open an excel file at the end of program and import all the data in the txt file into it directly.
Plz remember I am not talkin about writing into an .xls file, but how to start the excel application and open a new file (anyway we will get a new blank sheet only when we start it)and then do all this stuff.
is it possible to implement this?.. if it is, is it very hard doin by writing handler programs or so...? and how can i start writing values into tht file from a specific starting point from which i am interested to write?
any suggestions or advices r appreciated.
thanks in advance,
Suman
|
|
|
|
|
You want to develop an application that will talk to MS Excel, correct?
Kuphryn
|
|
|
|
|
yep u got my point.....but how can we make it....
Suman
|
|
|
|
|
Yes that can be done by using OLE AUTOMATION. Just search on net for key word "Office Automation" VC++. You'll get lots of articles.
|
|
|
|
|
thanks Sujan..,
I will look over thru some of those articles and if I am not able to figure it out... then i will let u know...i hope u understood my problem clearly...
though i am able to open an excel sheet in my vc++ program... i must be able to write the values tht my program generates into it from whereever i want to in tht sheet (i.e in some way i must be able to switch between rows and columns of the excel sheet).
cheers,
Suman
|
|
|
|
|
Hi
the better way to start is to use the macro option in office products. Record a macro, open it in visual basic editor, convert it to a VC++ structure, and use it. But converting the visual basic code to VC++ code will take some time.
I haven't worked with excel, but with ms word and outlook.
Hope this helps you
Sujan
|
|
|
|
|
|
Hi Azam,
thanks for ur advice, I have gone through the link tht u have given and created an example project based upon those tutorial. Actually its an MFC application and it creates a dialog box on which we have to click on 'run' which then activates the excel.
but wht i am trying to do is .. my program is a pire c++ source file. it generates certain values in the time of its execution and at present i am writing them into an text file. But i want to open an excel sheet (just like i opened a text file) and then i have to write the values tht i generate..into the excel sheet (just like i write the values whenever generated into a text file). and then i have at the end of the execution i have show up the excel sheet.
one more thing is I'm willing to write the data in the sheet from a particular row and column. My data is a 2D data (256x256).
hope i am clear to u...if u r having any ideas lemme know i'll work on it.
thanks in advance,
Pavan.
Suman
|
|
|
|
|
ya i got it but wud you plz confirm me one thing, what kind of application you are developing, i mean is it a MFC based application (using CFile or some derivative of it for opening and writing text file) or a WIN32 application (using HFILE for opening and writing text file) or core C++ (using FILE structure for opening and writing text file).
As far as i know in either case you can use COM (or specifically Automation) for your application.
Also the link that i sent to you shows how you can write data in cells of excel sheet. were you able to run that program since if you are using excel 2002 or above then i suppose you have to change the line range.SetValue(COleVariant(saRet)); to range.SetValue2(COleVariant(saRet)); in your run method
any ways will try to figure out some thing for you
regards
Azam
|
|
|
|
|
Hi Azam,
I am writing just a common cpp source file and it is not any MFC or win32 application.I am using 'ofstream' to create text file and writing the values into it. My program goes like this....
#include <fstream.h>
#include <iostream.h>
#include <math.h>
#include <conio.h>
#define phi 3.1412
void main()
{.....
ofstream out_file("data.txt");
for(...)
{
for(...)
{.....
out_file<<",,,,";
}
}
....
}
yep i have ran tht tutorial program and it is indeed writing the values into excel..... but the only thing is completely dialog based and we have to enter values into the dialog box and then it writes it into excel sheet. But, here i have to write them directly into the excel sheet as and when i generate a value in my program..
hope i answered ur question in a right way...
thanks for ur help,
Suman
|
|
|
|
|
Hi Suman
Create a Win32 Console application project and paste the following code in the file containing main function donot forget to remove all things from that file b4 pasting
hope you will be able to customize this code according to your need what i suggest try to find the nested for loop that is writing in cells of excell file and in that nested for loop generate you data and assign it to the variable that is being written to the file;
One more suggestion try to encapsulate this procedureal code into some class for elegent look. and expose method like FIllCell(row,col,data) ..any ways its upto you further investigation of this code will also enable you to save the excel file on disk;
#include "stdafx.h"
#include <ole2.h>
// AutoWrap() - Automation helper function...
HRESULT AutoWrap(int autoType, VARIANT *pvResult, IDispatch *pDisp, LPOLESTR ptName, int cArgs...) {
va_list marker;
va_start(marker, cArgs);
if(!pDisp) {
MessageBox(NULL, "NULL IDispatch passed to AutoWrap()", "Error", 0x10010);
_exit(0);
}
DISPPARAMS dp = { NULL, NULL, 0, 0 };
DISPID dispidNamed = DISPID_PROPERTYPUT;
DISPID dispID;
HRESULT hr;
char buf[200];
char szName[200];
WideCharToMultiByte(CP_ACP, 0, ptName, -1, szName, 256, NULL, NULL);
hr = pDisp->GetIDsOfNames(IID_NULL, &ptName, 1, LOCALE_USER_DEFAULT, &dispID);
if(FAILED(hr)) {
sprintf(buf, "IDispatch::GetIDsOfNames(\"%s\") failed w/err 0x%08lx", szName, hr);
MessageBox(NULL, buf, "AutoWrap()", 0x10010);
_exit(0);
return hr;
}
VARIANT *pArgs = new VARIANT[cArgs+1];
for(int i=0; i<cArgs; i++) {
pArgs[i] = va_arg(marker, VARIANT);
}
dp.cArgs = cArgs;
dp.rgvarg = pArgs;
if(autoType & DISPATCH_PROPERTYPUT) {
dp.cNamedArgs = 1;
dp.rgdispidNamedArgs = &dispidNamed;
}
hr = pDisp->Invoke(dispID, IID_NULL, LOCALE_SYSTEM_DEFAULT, autoType, &dp, pvResult, NULL, NULL);
if(FAILED(hr)) {
sprintf(buf, "IDispatch::Invoke(\"%s\"=%08lx) failed w/err 0x%08lx", szName, dispID, hr);
MessageBox(NULL, buf, "AutoWrap()", 0x10010);
_exit(0);
return hr;
}
va_end(marker);
delete [] pArgs;
return hr;
}
int main(int argc, char* argv[])
{
CoInitialize(NULL);
CLSID clsid;
HRESULT hr = CLSIDFromProgID(L"Excel.Application", &clsid);
if(FAILED(hr)) {
::MessageBox(NULL, "CLSIDFromProgID() failed", "Error", 0x10010);
return -1;
}
IDispatch *pXlApp;
hr = CoCreateInstance(clsid, NULL, CLSCTX_LOCAL_SERVER, IID_IDispatch, (void **)&pXlApp);
if(FAILED(hr)) {
::MessageBox(NULL, "Excel not registered properly", "Error", 0x10010);
return -2;
}
{
VARIANT x;
x.vt = VT_I4;
x.lVal = 1;
AutoWrap(DISPATCH_PROPERTYPUT, NULL, pXlApp, L"Visible", 1, x);
}
IDispatch *pXlBooks;
{
VARIANT result;
VariantInit(&result);
AutoWrap(DISPATCH_PROPERTYGET, &result, pXlApp, L"Workbooks", 0);
pXlBooks = result.pdispVal;
}
IDispatch *pXlBook;
{
VARIANT result;
VariantInit(&result);
AutoWrap(DISPATCH_PROPERTYGET, &result, pXlBooks, L"Add", 0);
pXlBook = result.pdispVal;
}
VARIANT arr;
arr.vt = VT_ARRAY | VT_VARIANT;
{
SAFEARRAYBOUND sab[2];
sab[0].lLbound = 1; sab[0].cElements = 15;
sab[1].lLbound = 1; sab[1].cElements = 15;
arr.parray = SafeArrayCreate(VT_VARIANT, 2, sab);
}
for(int i=1; i<=15; i++) {
for(int j=1; j<=15; j++) {
VARIANT tmp;
tmp.vt = VT_I4;
tmp.lVal = i*j;
long indices[] = {i,j};
SafeArrayPutElement(arr.parray, indices, (void *)&tmp);
}
}
IDispatch *pXlSheet;
{
VARIANT result;
VariantInit(&result);
AutoWrap(DISPATCH_PROPERTYGET, &result, pXlApp, L"ActiveSheet", 0);
pXlSheet = result.pdispVal;
}
IDispatch *pXlRange;
{
VARIANT parm;
parm.vt = VT_BSTR;
parm.bstrVal = ::SysAllocString(L"A1:O15");
VARIANT result;
VariantInit(&result);
AutoWrap(DISPATCH_PROPERTYGET, &result, pXlSheet, L"Range", 1, parm);
VariantClear(&parm);
pXlRange = result.pdispVal;
}
AutoWrap(DISPATCH_PROPERTYPUT, NULL, pXlRange, L"Value", 1, arr);
::MessageBox(NULL, "All done.", "Notice", 0x10000);
{
VARIANT x;
x.vt = VT_I4;
x.lVal = 1;
AutoWrap(DISPATCH_PROPERTYPUT, NULL, pXlBook, L"Saved", 1, x);
}
AutoWrap(DISPATCH_METHOD, NULL, pXlApp, L"Quit", 0);
pXlRange->Release();
pXlSheet->Release();
pXlBook->Release();
pXlBooks->Release();
pXlApp->Release();
VariantClear(&arr);
CoUninitialize();
return 0;
}
hope this time i am on right direction
regards
Azam
|
|
|
|
|
oh there was a typo mistake
in HRESULT AutoWrap(int autoType, VARIANT *pvResult, IDispatch *pDisp, LPOLESTR ptName, int cArgs...)
// Extract arguments...
for(int i=0; i pArgs[i] = va_arg(marker, VARIANT);
}
replace these lines with these
for(int i=0; i < cArgs; i++) {
pArgs[i] = va_arg(marker, VARIANT);
}
|
|
|
|
|
Hi Azam,
thank you very much... it seems u have spent some of ur valuable time for my program.... at sometime today, i will try to include it in my program and make necessary changes and let you know the consequences.
once again thanking you,
Pavan.
Suman
|
|
|
|
|