Problem with DBL_SetMessageShowHandler and c#

athomaathoma Member Posts: 5
edited 2004-06-15 in Navision Attain
Hi to all,

i have casting DBL_SetMessageShowHandler in c#, this works fine, but
only first time :( , at second error my function is not execute.
Any ideas?

regards, Arek

P.S. sorry for my bad english
using System;
using System.Runtime.InteropServices; 

namespace TestAppNav
{
	delegate  void  _ErrorShowProc(string Message, int MessageType,  string  ErrorCode);

	class Class1
	{
		[DllImport("cfront.dll", CharSet=CharSet.Ansi , CallingConvention=CallingConvention.Cdecl) ]
		private static extern  IntPtr DBL_SetMessageShowHandler(_ErrorShowProc ShowFunc);
		private static _ErrorShowProc oNavErr;
		private static string sMsg=null;
		static void  myErrorShowProc(string Message,  int MessageType, string  ErrorCode)
		{
			//example
			sMsg = Message.ToString();// Char2Oem(Message.ToString());
			Message = null;
		}
		[DllImport("cfront.dll",  SetLastError = true, CallingConvention = CallingConvention.Cdecl) ]
		private static extern void DBL_ConnectServer( string ServerName, string NetType );
		[DllImport("cfront.dll",  SetLastError = true, CallingConvention = CallingConvention.Cdecl) ]
		private static extern bool DBL_Init();
		[DllImport("cfront.dll",  SetLastError = true, CallingConvention = CallingConvention.Cdecl) ]
		private static extern void DBL_Exit();
		[DllImport("cfront.dll",  SetLastError = true, CallingConvention = CallingConvention.Cdecl) ]
		private static extern void DBL_OpenDatabase(string DatabaseName, int CacheSize, bool UseCommitCache);
		[DllImport("cfront.dll",  SetLastError = true, CallingConvention = CallingConvention.Cdecl) ]
		private static extern void DBL_OpenCompany(char[] CompanyName);
		[DllImport("cfront.dll",  SetLastError = true, CallingConvention = CallingConvention.Cdecl) ]
		private static extern void DBL_CloseDatabase(); 
		[DllImport("cfront.dll",  SetLastError = true, CallingConvention = CallingConvention.Cdecl) ]
		private static extern string DBL_CompanyName(); 
		[DllImport("cfront.dll",  SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
		private static extern Int32 DBL_GetVersion(); 
		[DllImport("cfront.dll",  SetLastError = true, CallingConvention = CallingConvention.Cdecl) ]
		private static extern void DBL_Ansi2OemBuff ([In,Out] char[] Src, [In,Out] char[] Dst,int DstSize);
		[DllImport("cfront.dll",  SetLastError = true, CallingConvention = CallingConvention.Cdecl) ]
		private static extern void DBL_Oem2AnsiBuff ([In,Out] byte[] Src, [In,Out] byte[] Dst,int DstSize);

		[STAThread]
		static void Main(string[] args)
		{
			bool bOk = DBL_Init();
			oNavErr = new _ErrorShowProc(myErrorShowProc);
			IntPtr nPointer = DBL_SetMessageShowHandler(oNavErr);
			GC.KeepAlive(oNavErr);

			OpenDatabase("D:\\_Navision\\Navision_Datenbanken\\databaseNavDemo370.fdb", 9000, true);
			//DBL_ConnectServer("192.168.1.28", "TCP");
			//first Error -> ok
			OpenCompany("CRONUS 999999 AG");
			Console.WriteLine("Navision Error " + sMsg);
			//second error -> nothing
			OpenCompany("CRONUS 88888 AG");
			Console.WriteLine("Navision Error " + sMsg);
				Console.WriteLine( "Version {0}", DBL_GetVersion() );  
			DBL_Exit();
		}

		public static bool OpenCompany(string p_CompanyName)
		{
			try 
			{

				int nSizeLoc = p_CompanyName.Length;

				char[] myData = new char[nSizeLoc];
				char[] myDest = new char[nSizeLoc];

				myData= p_CompanyName.ToCharArray();

				DBL_Ansi2OemBuff(myData, myDest, nSizeLoc);
				DBL_OpenCompany(myDest);
				return true;
			}
			catch
			{
				return false;
			}

		}
		public static bool OpenDatabase(string p_DatabaseName, int p_CacheSize, bool p_UseCommitCache)
		{
			try 
			{
				DBL_OpenDatabase(p_DatabaseName, p_CacheSize, p_UseCommitCache);
				return true;
			}
			catch
			{
				return false;
			}
		}
	}

}
Sign In or Register to comment.