Developing for Dynamics GP

by David Musgrave (Perth, WA, Australia) and the Microsoft Dynamics GP Developer Support Team (Fargo, ND, USA)

Working with Temporary Files in Dexterity

Working with Temporary Files in Dexterity

  • Comments 5

David Meego - Click for blog homepageKeeping with the theme of my previous post, I have another "Temporary" topic to discuss.

Dexterity developers sometimes need to write out files (usually text files) to a location.  This could be for a variety of purposes, including:

  • to create a file for integration into an external application in Extensible Markup Language (.XML), Tab delimited (.TXT), Comma Separated Variable (.CSV) or Hypertext Markup Language (.HTML) format.
  • to write a Macro (.MAC) file to be executed on-the-fly. 
  • to write a Batch (.BAT) file with commands to be run at the command prompt level.

The problem is that we need a location that we have write permissions to. Remember that UAC (User Access Control) could prevents us using the application folder.  We also need a location that is not used by other users, so we can avoid two users clashing if they try to create the same file at the same time in the same location.

That situation is fairly easily solved by creating the file in the user's temporary (TEMP) folder.  But how do we know what the path is to the temporary folder from inside Dexterity.

Well, if you are using Dexterity 11.0 for Microsoft Dynamics GP 2010, the answer is really simple.  There is a new function library command File_GetTempDirectory() that returns the path in Native format. For example: "C:\Users\{UserName}\AppData\Local\Temp\". 

Note: You might need to convert this path to Generic format with Path_MakeGeneric()for some functions.  Make sure you read the Dexterity Help File to understand which functions need a Generic format pathname.


So, what can you do if you are using an older version of Dexterity?

You can call out to a Win32 function to ask the operating system to get the temp folder path and pass it back to you. This article will discuss the methods involved for this technique as they can be used for other calls to the Win32 library DLL (Dynamic Link Library) files available in Windows.

To achieve this there are 2 steps, but I normally make it into 3 steps by adding a wrapper function to simplify the calls:

  1. Create a prototype procedure for the call.
  2. Create a wrapper function for the call.
  3. Call the wrapper function.

Before we can start, we need to know the parameters of the Win32 function we wish to use, usually a quick search will find the MSDN page for you. 

In our case http://msdn.microsoft.com/en-us/library/aa364992(VS.85).aspx gives the following syntax:

DWORD WINAPI GetTempPath(
  __in   DWORD nBufferLength,
  __out  LPTSTR lpBuffer
);

Note: We are going to use the ANSI version of the command as we don't support unicode (double byte).  So this is the GetTempPathA() function.

Step 1: Prototype Procedure

The prototype procedure is a global procedure named the same as the Win32 function and having the same parameters as the Win32 function.  Notice the addition of the long integer as the first parameter to capture the returned status from the Win32 function.

Note: In our case the GetTempPathA@kernel32.dll global procedure already exists in the core Dynamics.dic dictionary and so we can skip this step.

Global Procedure: GetTempPathA@kernel32.dll

Step 2: Wrapper Function

The wrapper function calls the prototype procedure using the extern command and provides a nice user friendly interface for our code to use.  Including the MSDN link as a comment is handy if you need to reference the page again.

Global Function: GetTempPath

Step 3: Calling the Function 

Now we have our wrapper function in place, we can call it as desired. 

Code Example

A final point. Always remember to delete your temporary files when you have finished with them.

I hope you find this article useful as it explains both how to get the path to the user's temporary folder and the method for calling external Win32 DLLs.

David

// Copyright © Microsoft Corporation.  All Rights Reserved.
// This code released under the terms of the
// Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)

  • Posting from Mark Polino from DynamicAccounting.net

    msdynamicsgp.blogspot.com/.../working-with-temporary-files-in.html

  • I am having problem with CreateFileA@Kernel32.dll

  • Hi Hammad.

    Can I suggest logging a support case or asking on the forums.

    When you do, please give details.

    Is there a reason you are not using the built in commands for working with files?

    David

  • Wanted to write a CSV file but found a better approach of exporting through the Report.

    Thanks

  • Hi Hammad

    You can also use the TextFile_ function library commands and just build up each line of the table in a text variable and write it out.

    I have just used this method on a project.

    David

Page 1 of 1 (5 items)
Comments Information

PLEASE READ BEFORE POSTING

Please only post comments relating to the topic of this page.

If you wish to ask a technical question, please use the links in the links section (scroll down, on right hand side) to ask on the Newsgroups or Forums. If you ask on the Newsgroups or Forums, others in the community can respond and the answers are available for everyone in the future.

Leave a Comment
  • Please add 6 and 6 and type the answer here:
  • Post