Hi all,
I have a windows service that is calling a function from an unmanaged dll.
The C++ declaration is : (char *) getxmlRequest(const char *szHostName,
unsigned short usPort, const int nNetTimeout, void *xml, int size);
My C# declarations are:
[DllImport("xmlinterop.dll",
EntryPoint="getxmlRequest",ExactSpelling=false,CallingConvention=CallingConvention.StdCall,SetLastError=true)]
static extern string getxmlRequest([MarshalAs(UnmanagedType.LPStr)] string
server,UInt16 port,Int32 timeout,[MarshalAs(UnmanagedType.LPStr)] string buf
,Int32 length);
If I run this code in a WIndows application, I have no errors, but running
in the service application I get:
"Object reference not set to an instance of an object".
Any ideas?
Thanks,
Doru
Robert Jordan - 19 Aug 2004 14:36 GMT
hi doru,
> If I run this code in a WIndows application, I have no errors, but running
> in the service application I get:
> "Object reference not set to an instance of an object".
>
> Any ideas?
where is the exception exactly coming from? please post the
whole exception with its stacktrace.
i'm supposing the unmanaged code doesn't cope well
when called by a service. it might access the registry
or something not available to a service.
bye
rob
Doru - 22 Aug 2004 00:39 GMT
Hi Robert,
I think it is a problem of Garbage Collector.
I will try GC.KeepAlive(returnedString)
Doru
> hi doru,
>
[quoted text clipped - 13 lines]
> bye
> rob
Phil Wilson - 22 Aug 2004 19:34 GMT
Output strings are usually interoped as StringBuilder. Declare it as
StringBuilder, then you'd allocate it first then use it as the return
result. Something like:
StringBuilder ret = new StringBuilder (100);
ret = getxmlRequest(...)

Signature
Phil Wilson
[MVP Windows Installer]
> Hi Robert,
>
[quoted text clipped - 20 lines]
> > bye
> > rob