Re: Problem calling function in DLL via NETLink: NET::netexcptn: A .NET exception occurred
- To: mathgroup at smc.vnet.net
- Subject: [mg66341] Re: [mg66319] Problem calling function in DLL via NETLink: NET::netexcptn: A .NET exception occurred
- From: Todd Gayley <tgayley at wolfram.com>
- Date: Wed, 10 May 2006 06:33:43 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
At 01:35 AM 5/9/2006, Heiko Damerau wrote: >Hello, > > I am trying to call a function in an ordinary C++ DLL (should be >well tested, not my code). The DLL function I am calling is declared as > > short SyncRPC FAR PASCAL SyncRPC(const char *locName, const char >*locProperty, const char *locCycle, short ArraySize, void *Data, long >DataSize, short DataFormat, short DataType) > > As I do not need the full flexibility when called from Mathematica, >it is defined as > > DLLFunctionSyncRPC = DefineDLLFunction["SyncRPC", "rpcapi32.dll", >"short", {"char*", "char*", "char*", "short", "byte[]", "byte", "short", >"short"}]; > >and finally called by > > DLLFunctionSyncRPC[elementName, property, plsLine, optArraySize, >netManagedDataArray, 0, 1, 0]; > >where elementName, property, plsLine are Mathematica strings, >optArraySize is an ordinary mathematica number (integer) and >netManagedDataArray is a .NET object declared as > > NETNew["System.Byte[]", 17optArraySize + 1]; > >to get the pointer to the result. Everything works fine but sometimes I >get strange error messages: > >NET::netexcptn: A .NET exception occurred: >System.NullReferenceException: Object reference not set to an instance >of an object. at >Wolfram.NETLink.DynamicDLLNamespace.DLLWrapper612.SyncRPC(String , >String , String , Int16 , Byte[] , Byte , Int16 , Int16 ) > > and my notebook is not executed as it should. Changing nothing and >just running the Mathematica notebook a second time and the error >disappears most of the time. The error appears especially, when I call >the DLL function extremely often; but also with a fresh kernel the error >normally comes after some 25 calls of the function in the DLL. > > It would be great if anyone could give me a hint what is wrong with >my function calls, because most of the time everything works correctly. Heiko, One problem is that the "byte" type (DataSize) in your DefineDLLFunction should be "long." Todd Gayley Wolfram Research