Thursday, December 21, 2006

Memory usage of axapta task

On a job client we had some troubles with memory wastage.
The reason was an external application with which we communicate across an OCX.
On each call the application needs more and more memory.
Our experience shows, on about 100MB Axapta crashed.
To solve this problem, our idea was, to monitor the used memory and if axapta reaches an defined limit, to shut down the axapta client.
With windows task manager it should be restartet automatic.

Now, after approximately three weeks in production system, we can say it works without problems, it works marvelously.


Solution:
Three new WinAPI-functions:


getProcessMemoryInfo
Retrieves information about the memory usage of the specified process.
Link to ms...


static container getProcessMemoryInfo(int _processHandle)
{
#define.structSize(4*10)
DLLFunction _getProcessMemoryInfo ;
Binary struct = new Binary(#structSize);
DLL _dLL = new DLL(#PSAPIDLL);
_getProcessMemoryInfo = new DLLFunction(_DLL, 'GetProcessMemoryInfo');

_getProcessMemoryInfo.returns(ExtTypes::DWord);

_getProcessMemoryInfo.arg(ExtTypes::DWord,
ExtTypes::Pointer, ExtTypes::DWord);

struct.dWord (#offset0, 0 );
struct.dWord (#offset4, #structSize );
struct.dWord (#offset8, 0 );
struct.dWord (#offset12, 0 );
struct.dWord (#offset16, 0 );
struct.dWord (#offset20, 0 );
struct.dWord (#offset24, 0 );
struct.dWord (#offset28, 0 );
struct.dWord (#offset32, 0 );
struct.dWord (#offset36, 0 );

if (_getProcessMemoryInfo.call(_processHandle, struct, #structSize))
{
return [struct.dWord(#offset0),
struct.dWord(#offset4),
struct.dWord(#offset8),

struct.dWord(#offset12),
struct.dWord(#offset16),
struct.dWord(#offset20),
struct.dWord(#offset24),
struct.dWord(#offset28),
struct.dWord(#offset32),
struct.dWord(#offset36)];
}

return conNull();

}


getCurrentProcessId
Retrieves the process identifier of the calling process.
Link to ms...

client server static int getCurrentProcessId()
{
//The return value is the process identifier of the calling process.

DLL winApiDLL = new DLL(#KERNELDLL);
DLLFunction getCurrentProcessId = new DLLFunction(winApiDLL, 'GetCurrentProcessId');

getCurrentProcessId.returns(ExtTypes::DWord);
return getCurrentProcessId.call();
}

OpenProcess
Opens an existing local process object.
Link to ms...

static int openProcess(boolean _openForQuery = true, int _processId = winapi::getCurrentProcessId())
{
//If the function succeeds, the return value is an open handle of the specified process.

#define.PROCESS_QUERY_INFORMATION (1024) //&h400
#define.PROCESS_VM_READ (16) //&h10
#define.PROCESS_ALL_ACCESS (2035711) //&H1F0FFF
DLL _winApiDLL = new DLL('KERNEL32');
DLLFunction _openProcess = new DLLFunction(_winApiDLL, 'OpenProcess');
int call = _openForQuery ? #PROCESS_QUERY_INFORMATION : #PROCESS_ALL_ACCESS;
;
_openProcess.returns(ExtTypes::DWord);
_openProcess.arg(ExtTypes::DWord, ExtTypes::DWord, ExtTypes::DWord);
return _openProcess.call(call, 0, _processId);
}

Usage:
Simple job:
static int getCurrentMemoryUsage(boolean _kiloByte = true)
{
hwnd curhwnd = winapi::openProcess();
container memInfo;
;
memInfo = winapi::getProcessMemoryInfo(curhwnd);
winapi::closeHandle(curhwnd);
if(_kiloByte)
return conpeek(memInfo, 4) / 1024;
else
return conpeek(memInfo, 4);
}

Thursday, December 07, 2006

RunBuf (Evaluate), a wonderful function

"RunBuf" or in other development environment "evaluate" is a very nice function,
it helps me to save a lot of time for to develop a complex parameter table stucture

Call the function below with something like this
evaluateFormularWithCommon(SalesTable::find("252"), "SalesTable::find(_common.(fieldnum(SalesTable, SalesId))).SalesName")
you get the value of the field "Name" of the salesTable!

This looks like an stupid idea, but in some situation it could be very helpfully!
In our situation i have an common as table variable and an string which comes from an parameter table.
So the function returns the value of the formular-string in the Parameter-table. Very Nice!



static container evaluateFormularWithCommon(Common _common,
str _formular)
{
/*
franz aigner 20061127
returnvalue:
[Result (str), ReturnState (boolean)]
ReturnState: False --> Error, True --> OK

Instruction:
Sampleformula:
SalesTable::find(_common.(fieldnum(SalesTable, SalesId))).SalesName


in this sample, in the formula the reference of the source table must be the string '_common'
no semicolon at the end of the formular
the return value must be a string
*/
XPPCompiler comp = new XPPCompiler();
str formular;

str toStr(AnyType _value)
{
;
if(typeOf(_value)==Types::String || typeOf(_value)==Types::VarString)
{
return strFmt("%1", _value);
}
return strFmt("%1", _value);
}
;
formular = 'AnyType evaluateFormularWithCommon(Common _common){return '+_formular+";}";

if(comp.compile(formular))
return [tostr(runbuf(formular, _common)), True];
else
return ["", false];
}



Navison Axapta Help:
RunBuf

Anytype RunBuf(str job, ...)

Description
RunBuf executes the X++ job represented as text in job.
Job is a complete piece of X++ code and arguments can be specified by
appending them as arguments to RunBuf.
If the job returns a value, this value is the return value from RunBuf.
Otherwise RunBuf returns 0 (zero).

Example
str myjob = "int myfunc(int i){ return i+7;}";
print runbuf(myjob,5);
will print the number 12 when executed.


Info:
This idea comes from tabax X++calculator!!


Friday, November 10, 2006

WTSQuerySessionInformation

The WTSQuerySessionInformation function retrieves session information for the specified session on the specified terminal server.

With the function below you can find out the ip-Address of the client-computer if they logged in on a terminal server!
Under it, there is a function to get the client machine name!

Take a look to
http://www.microsoft.com/Businesssolutions.....
http://msdn.microsoft.com/library/...

#define.WTSClientIP(14)
#define.WTS_CURRENT_SESSION(-1)
#define.WTS_CURRENT_SERVER_HANDLE(0)
#define.structSize(256)
client server static str getRDPIPAddress()
{
DLL _winApiDLL = new DLL('WTSAPI32');
DLLFunction _getRDPName = new DLLFunction(_winApiDLL, 'WTSQuerySessionInformationA');
Binary dwPointer;
Binary structPointer;
Binary structAccess;
int spRet;
int dwret;
str rdpIP = "";
;
structpointer = new Binary(4); // will receive the pointer to result struc
dwPointer = new Binary(4); // wiil receive the length of reult struc


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


if(_getRDPName.call(#WTS_CURRENT_SERVER_HANDLE, #WTS_CURRENT_SESSION,
#WTSClientIP, structpointer, dwpointer) != 0)
{
spRet = structpointer.dWord(0);
dwRet = dwPointer.dWord(0);
structAccess = new Binary(0);
structAccess.attach(spRet,dwRet);

rdpIP = strfmt("%1", structAccess.byte(6));
rdpIP = rdpip + "." + strfmt("%1", structAccess.byte(7));
rdpIP = rdpip + "." + strfmt("%1", structAccess.byte(8));
rdpIP = rdpip + "." + strfmt("%1", structAccess.byte(9));
}
WinAPI::WTSFreeMemory(spRet);
return rdpIP;
}

client static void WTSFreeMemory(int _buffer)
{
DLL wtsapi32DLL = new DLL('wtsapi32.dll');
DLLFunction wtsfm = new DLLFunction(wtsapi32DLL, 'WTSFreeMemory');
;
wtsfm.arg(ExtTypes::DWord);
wtsfm.call(_buffer);
}

To get the client machine name:

#define.WTSClientName(10)
client server static str getRDPComputerName()
{
/*
Franz Aigner 20050922

*/
DLL _winApiDLL = new DLL('WTSAPI32');
DLLFunction _getRDPName = new DLLFunction(_winApiDLL, 'WTSQuerySessionInformationA');
Binary dwPointer;
Binary structPointer;
Binary structAccess;
int spRet;
int dwret;
str rdpName = "";
;
structpointer = new Binary(4); // will receive the pointer to result struc
dwPointer = new Binary(4); // wiil receive the length of reult struc


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

if(_getRDPName.call(#WTS_CURRENT_SERVER_HANDLE, #WTS_CURRENT_SESSION,
#WTSClientName, structpointer, dwpointer) != 0)
{
spRet = structpointer.dWord(0);
dwRet = dwPointer.dWord(0);
structAccess = new Binary(0);
structAccess.attach(spRet,dwRet);
rdpName = structAccess.string(0);
}
WinAPI::WTSFreeMemory(spRet);
return rdpName;
}

Monday, November 06, 2006

Find out the string length of an extendedDataType!

Find out the string length of an extendedDataType --> was necessary for me as i wrote an select-statement with the connection-class.

static void TestJob(Args _args)

{

Dictionary dict;

DictType dictType;

;

dict = new Dictionary();

dictType = dict.typeObject(dict.typeName2Id(extendedtypestr(AccountName)));

info(strfmt("Name : %1 \nId: %2 \nStringLength: %3 \nAdjustment: %4 \nLabel: %5 \nHelp: %6 \nBasetype: %7", dictType.name(), dictType.id(), dictType.stringLen(), dictType.stringRight() ? "Right" : "Left", dictType.label(), dictType.help(), int2str(dictType.baseType()) + " - " + enum2Value(dictType.baseType())));
}

Thursday, November 02, 2006

MakeSureDirectoryPathExists

The powerfully WinApi-Function 'MakeSureDirectoryPathExists' implementet in X++!

The MakeSureDirectoryPathExists function creates all the directories in the specified _filePath, beginning with the root.

This function resides in imagehlp.dll, but also in
dbghelp.dll.
Take a look to http://msdn2.microsoft.com/en-us/library/ms680352.aspx

static int MakeSureDirectoryPathExits(FilePath _filePath)
{
/*
fa 20061102
Returnvalue:
0... OK
>0.. Error
*/

#define.imagehlp('imagehlp.dll')
DLL winApiDLL = new DLL(#imagehlp);
DLLFunction pathExits = new DLLFunction(winApiDLL, 'MakeSureDirectoryPathExists');
int retVal = 0;
;
if(substr(_filePath, strlen(_filePath), 2) != "\\")
_filePath += "\\";


pathExits.returns(ExtTypes::DWord);
pathExits.arg(ExtTypes::String);

if (pathExits.call(_filePath) == 0)
{
retVal = WinAPI::getLastError();
retVal = retVal == 0 ? 1 : retVal;
}

return retVal;

}

Wednesday, November 01, 2006

First-Blog

In my programmer career I looked for and also found solutions to problems often times in the Internet.
Therefore I think, it also for me at the time to perform a contribution for the Internet community.
In this Blog I would like to publish ideas, solutions and also program code about Axapta/Dynamics AX programming.
#####################################################
In meiner Programmiererlaufbahn habe ich oftmalig im Internet Lösungen zu Problemen gesucht und auch gefunden.

Daher denke ich, ist es auch für mich an der Zeit einen Beitrag für die Internetgemeinschaft zu leisten.

In diesem Blog möchte ich Ideen, Lösungsansätze und auch Programmcode zum Thema Axapta/Dynamics AX Programmierung veröffentlichen.

lg Franz