Loading a VDF program in C << Back



We talked about how DFRun works. Let's see some code in action as to how to load a DF 19.0 program/runtime into your C program. Compile the following program, and place the final EXE in the same directory as your Order Entry Example's Program folder. This program is just a proof of concept, definitely not production grade. This is to illustrate the point on how DFRun works.

So, the big question comes - why would I want to do that? An example would be that you need to host a VDF program in the same process as your C program so that you can communicate with it more easily (Instead of doing IPC) since both programs now live in the same process address space. By the way, if you attempt to spin up multiple threads to load multiple VDF programs into the same process, it will result in a crash.

#include <windows.h>

LPCTSTR REGPATH = TEXT("Software\\Data Access Worldwide\\Dataflex\\19.0\\DEFAULTS");
LPCTSTR RUNTIME = TEXT("vdfvm19.dll");
const char*  DLLFUNCTION = "_FlexRunProgramEx@4";

typedef BOOL(WINAPI *FRPE)(LPVOID);

void VDFRun()
{
	TCHAR szPath[2000];
	HKEY hKey;
	DWORD dwType, dwSize;
	HMODULE hRunTime;
	FRPE lpRunTime;
	HANDLE hFile, hMap;
	LPVOID lpFile;
	if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, REGPATH, 0, KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS)
	{
		dwType = REG_SZ;
		dwSize = 2000 * sizeof(TCHAR);
		if (RegQueryValueEx(hKey, TEXT("VDFRootDir"), NULL, &dwType, (BYTE*)szPath, &dwSize) == ERROR_SUCCESS)
		{
			dwSize /= sizeof(TCHAR);
			lstrcpy(&szPath[dwSize - 1], TEXT("Bin;"));
			GetEnvironmentVariable(TEXT("PATH"), &szPath[dwSize + 3], 2000 - dwSize);
			SetEnvironmentVariable(TEXT("PATH"), szPath);
		}
		RegCloseKey(hKey);
	}
	hRunTime = LoadLibrary(RUNTIME);
	if ((DWORD)hRunTime)
	{
		lpRunTime = (FRPE)GetProcAddress(hRunTime, DLLFUNCTION);
		if (lpRunTime)
		{
			hFile = CreateFile(TEXT("Order.exe"), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
			hMap = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
			lpFile = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0);
			lpRunTime((LPVOID)(((DWORD)lpFile) + 8192));
			UnmapViewOfFile(lpFile);
			CloseHandle(hMap);
			CloseHandle(hFile);
		}
	}
}
Free Web Hosting