[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.axapta.programming

Wait for action to complete

Jens Strandberg

10/31/2005 8:57:00 AM

From Axapta (Ax3.0 SP3) I am generating an Excel sheet, which is then
supposed to be e-mailed, also from Axapta.

How do I ensure that Excel is finished and closed, before I attempt to send
the generated file ?

I have been looking at WinAPI:WaitForSingleObject, but it does not work, as
I expected it to.

Thanks in advance.

/Jens
6 Answers

Ricardo Venegas [MSFT]

11/3/2005 1:54:00 AM

0

Hi,

WaitForSingleObject should work if you have the process handle (see an
example in
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/creating_pro...).

Another option that you have is create a WScript.Shell object using COM, and
use this object to launch excel and wait for the process to end (see
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/wslrfexec...
for an example).

Ricardo Venegas[MSFT]
--

This posting is provided "AS IS" with no warranties, and confers no
rights.


"Jens Strandberg" <JensStrandberg@discussions.microsoft.com> wrote in
message news:CE981A35-6E1C-4481-AFA8-8A890C61D1A4@microsoft.com...
> From Axapta (Ax3.0 SP3) I am generating an Excel sheet, which is then
> supposed to be e-mailed, also from Axapta.
>
> How do I ensure that Excel is finished and closed, before I attempt to
> send
> the generated file ?
>
> I have been looking at WinAPI:WaitForSingleObject, but it does not work,
> as
> I expected it to.
>
> Thanks in advance.
>
> /Jens


Jens Strandberg

11/3/2005 2:25:00 PM

0

Thanks Ricardo,

From Axapta, I can''t seem to get hold of a handle. I am probably just doing
something wrong here.

If I use:

hdl = WinAPI::shellExecute(_ExcelFileToRun)

only "1" is returned, which is probably not the handle, and therefore I am
not able to make use of WaitForSingleObject.

Any pointer - or obviouos mistakes from my side... ?

Thanks.

/Jens


>

Luegisdorf

11/3/2005 3:39:00 PM

0

you could make a test loop

boolean inprogress;

// start your excel things here ..


while (inprogress)
{
inprogress = winapi::filelocked(yourfile); // if true, excel hasn''t
close the file ...
// may be you should build in a timer, just if Excel hangs, Axapta will
hang too ...
}

Best regards
Patrick
"Jens Strandberg" wrote:

> From Axapta (Ax3.0 SP3) I am generating an Excel sheet, which is then
> supposed to be e-mailed, also from Axapta.
>
> How do I ensure that Excel is finished and closed, before I attempt to send
> the generated file ?
>
> I have been looking at WinAPI:WaitForSingleObject, but it does not work, as
> I expected it to.
>
> Thanks in advance.
>
> /Jens

Mike Frank

11/3/2005 3:46:00 PM

0

You can''t use ShellExecute for that. It executes the process
asynchronously and just returns an error or success code.

You could use CreateProcess instead. An example for an Axapta wrapper
for this is listed below. The hProc variable (in my case this is a class
variable) will contain the process handle if the call was executed
successfully.

protected boolean createProcess(str _commandLine, str _currentDirectory)
{
#CBS_WinBase
#define.sizeOfprocInfo(16)
#define.sizeOfstartUpInfo(68)

DLLFunction createProcess = new DLLFunction(_winApiDLL,
"CreateProcessA");
Binary commandLine = new Binary(_commandLine);
Binary currentDirectory = new Binary(_currentDirectory);
Binary procInfo = new Binary(#sizeOfprocInfo);
Binary startUpInfo = new Binary(#sizeOfstartUpInfo);
boolean result;

startUpInfo.dWord(0, #sizeOfstartUpInfo); // DWORD cb;

createProcess.returns(ExtTypes::DWORD);
createProcess.arg(ExtTypes::DWORD,
ExtTypes::POINTER,
ExtTypes::DWORD,
ExtTypes::DWORD,
ExtTypes::DWORD,
ExtTypes::DWORD,
ExtTypes::DWORD,
ExtTypes::POINTER,
ExtTypes::POINTER,
ExtTypes::POINTER);

result = createProcess.call(null, commandLine, null, null, false,
/*#CREATE_NO_WINDOW*/ 0, null,
currentDirectory, startUpInfo, procInfo);

hProc = procInfo.dWord(0);

return result;

//BOOL CreateProcess(
// LPCTSTR lpApplicationName, // name of executable module
// LPTSTR lpCommandLine, // command line string
// LPSECURITY_ATTRIBUTES lpProcessAttributes, // SD
// LPSECURITY_ATTRIBUTES lpThreadAttributes, // SD
// BOOL bInheritHandles, // handle inheritance option
// DWORD dwCreationFlags, // creation flags
// LPVOID lpEnvironment, // new environment block
// LPCTSTR lpCurrentDirectory, // current directory name
// LPSTARTUPINFO lpStartupInfo, // startup information
// LPPROCESS_INFORMATION lpProcessInformation // process information
//);

//typedef struct _STARTUPINFO {
// DWORD cb;
// LPTSTR lpReserved;
// LPTSTR lpDesktop;
// LPTSTR lpTitle;
// DWORD dwX;
// DWORD dwY;
// DWORD dwXSize;
// DWORD dwYSize;
// DWORD dwXCountChars;
// DWORD dwYCountChars;
// DWORD dwFillAttribute;
// DWORD dwFlags;
// WORD wShowWindow;
// WORD cbReserved2;
// LPBYTE lpReserved2;
// HANDLE hStdInput;
// HANDLE hStdOutput;
// HANDLE hStdError;
//} STARTUPINFO, *LPSTARTUPINFO;

//typedef struct _PROCESS_INFORMATION {
// HANDLE hProcess;
// HANDLE hThread;
// DWORD dwProcessId;
// DWORD dwThreadId;
//} PROCESS_INFORMATION;

}

Mike Frank

11/3/2005 3:46:00 PM

0

I wanted to try the WScript.Shell version, but the call to
WScriptShell.Exec(..) gives me an error, saying that the variant type
returned by the function is not supported.

Any hint?

Mike

Mike Frank

11/7/2005 10:02:00 AM

0

Working now, my first attempt was with the simple calling technique. If using COMVariants it works.

COM wshShell = new COM(''WScript.Shell'');
COMDispFunction excecFunc = new COMDispFunction(wshShell, "Exec", COMDispContext::Method);
COMVariant parm = new COMVariant(COMVariantInOut::In, COMVariantType::VT_BSTR);
COMVariant retValue = new COMVariant(COMVariantInOut::Out_retVal, COMVariantType::VT_DISPATCH);
COM objExec;

parm.bStr("calc.exe");
excecFunc.call(parm, retValue);

objExec = COM::createFromVariant(retValue);

while (objExec.Status() == 0)
{
sleep(100);
}