Mar
19
Written by:
admin
Thursday, March 19, 2009
The execute Command is “OPEN[ExternalID]”
Where ExternalID is the external ID of the Patient
Cheers
// To Use --> Create new MD3DDEClient and Call Open, then Execute then Close.
using System;
using System.Collections.Generic;
using System.Text;
namespace MD3DDELink
{
using System;
using System.Runtime.InteropServices;
using System.Threading;
using DDELibrary;
///
/// Summary description for DDEClient.
///
public class MD3DDEClient : IDisposable
{
#region Delegates
private delegate IntPtr DDECallback(TransactionType uType, int uFmt, IntPtr hConv, IntPtr hsz1, IntPtr hsz2, IntPtr hdata, IntPtr data1, IntPtr data2);
#endregion
#region DllImports
[DllImport("user32.dll", EntryPoint = "DdeClientTransaction", CharSet = CharSet.Auto)]
private static extern IntPtr DdeClientTransactionString(string pData, int cbData, IntPtr hConv, IntPtr hszItem, ClipboardFormats wFmt, TransactionType wType, int dwTimeout,
int pdwResult);
[DllImport("user32.dll")]
private static extern IntPtr DdeConnect(int idInst, IntPtr hszService, IntPtr hszTopic, IntPtr pCC);
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
private static extern IntPtr DdeCreateStringHandleW(int idInst, string psz, int iCodePage);
[DllImport("user32.dll")]
private static extern int DdeDisconnect(IntPtr hc);
[DllImport("user32.dll")]
private static extern void DdeFreeDataHandle(IntPtr data);
[DllImport("user32.dll")]
private static extern int DdeFreeStringHandle(int idInst, IntPtr hsz);
[DllImport("user32.dll")]
private static extern DDEErrors DdeGetLastError(int idInst);
[DllImport("user32.dll")]
private static extern int DdeInitializeW(ref int id, DDECallback cb, int afcmd, int ulres);
[DllImport("user32.dll")]
private static extern int DdeUninitialize(int id);
#endregion
#region Fields
private IntPtr _ChannelID = IntPtr.Zero;
private int _InstanceID;
private bool _LastTimeout;
#endregion
#region Constructor
public MD3DDEClient()
{
DdeInitializeW(ref this._InstanceID, this.UselessDDECallback, 0x00008000 | 0x003c0000 | 0x00000010, 0);
if (this._InstanceID == 0)
{
throw new Exception("DdeInitialize failed!");
}
}
#endregion
#region Public Properties
public bool Opened
{
get { return this._ChannelID != IntPtr.Zero; }
}
public bool LastTimeout
{
get { return this._LastTimeout; }
}
#endregion
#region Public Methods
public void Close()
{
if (this._ChannelID != IntPtr.Zero)
{
DdeDisconnect(this._ChannelID);
this._ChannelID = IntPtr.Zero;
}
}
public bool Execute(string command, int timeout)
{
if (!this.Opened)
{
return false;
}
this._LastTimeout = false;
do
{
IntPtr da = DdeClientTransactionString(command, command.Length * 2 + 2, this._ChannelID, IntPtr.Zero, 0, TransactionType.XTYP_EXECUTE, timeout, 0);
if (da == IntPtr.Zero)
{
DDEErrors e = DdeGetLastError(this._InstanceID);
if (e == DDEErrors.BUSY)
{
Thread.Sleep(100);
continue;
}
else if (e == DDEErrors.EXECACKTIMEOUT)
{
this._LastTimeout = true;
break;
}
else
{
break;
}
}
else
{
DdeFreeDataHandle(da);
return true;
}
}
while (true);
return false;
}
public bool Open(string Service,string Topic)
{
IntPtr ipService = DdeCreateStringHandleW(this._InstanceID, "MDW", 1200);
IntPtr ipTopic = DdeCreateStringHandleW(this._InstanceID, "PATIENT", 1200);
this._ChannelID = DdeConnect(this._InstanceID, ipService, ipTopic, new IntPtr(0));
DdeFreeStringHandle(this._InstanceID, ipService);
DdeFreeStringHandle(this._InstanceID, ipTopic);
return this._ChannelID != IntPtr.Zero; }
#endregion
#region Methods
private IntPtr UselessDDECallback(TransactionType uType, int uFmt, IntPtr hConv, IntPtr hsz1, IntPtr hsz2, IntPtr hdata, IntPtr data1, IntPtr data2)
{
return IntPtr.Zero;
}
#endregion
#region IDisposable Members
public void Dispose()
{
this.Close();
if (this._InstanceID != 0)
{
DdeUninitialize(this._InstanceID);
this._InstanceID = 0;
}
}
#endregion
}
}
internal enum ClipboardFormats
{
CF_NONE = 0,
CF_TEXT = 1,
CF_BITMAP = 2,
CF_METAFILEPICT = 3,
CF_UNICODETEXT = 13
}
internal enum DDEErrors
{
NOTPROCESSED = 0x4009,
NOERROR = 0,
BUSY = 0x4001,
EXECACKTIMEOUT = 0x4005,
POKEACKTIMEOUT = 0x400b,
DATAACKTIMEOUT = 0x4002
}
internal enum TransactionType
{
XTYP_REGISTER = 0x00A0 | 0x8000 | 0x0002,
XTYP_UNREGISTER = 0x00D0 | 0x8000 | 0x0002,
XTYP_ADVDATA = 0x0010 | 0x4000,
XTYP_XACT_COMPLETE = 0x0080 | 0x8000,
XTYP_DISCONNECT = 0x00C0 | 0x8000 | 0x0002,
XTYP_EXECUTE = 0x0050 | 0x4000,
XTYP_REQUEST = 0x00B0 | 0x2000,
XTYP_POKE = 0x0090 | 0x4000
}