473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Marshal, Interop, other way?

I must use methods from standard dll im my C#code. In dll declaration of
method is like this:

typedef (__stdcall *ExecuteTaskFromXML)( const string& input, string&
output, DWORD& dwSize, LPVOID& pCtx );

How can I import (and use of course) this method? I try:
[DllImport("ExportDll.dll")]
public static extern void ExecuteTaskFromXML(String input, ref String
output, int dwSize, int pCtx);

but I've got error: System.NullReferenceException. (on String output)

What I must do? Some sample, docs or "entry point in codeproject" ;)) will
be best for me...

Please help

Regards
Krzysztof
Nov 15 '05 #1
4 3567
C++ "string" and .NET's String are two very different beasts. While, in the
sense of P/Invoke, you can declare arguments as System.String for all
flavours of char*, TCHAR*, TSTR and so on, this trick won't work for the C++
"string" class because it is not a mere pointer to a sequence of characters
in memory.

You can do a trick by declaring a managed structure having the same layout
in memory as the C++ string class, but still it can cause many troubles
related to marshalling string data. If you have at least slim chance to
alter the unmanaged DLL function to return some API-compatible string type
like TCHAR, use it!

One more thing - the unmanaged function is declared as "__stdcall". I am not
sure this is the same that the "WINAPI" convention expected by the .NET's
P/Invoke by default.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Krzysztof" <kk******@poczta.onet.pl> wrote in message
news:bq**********@news.onet.pl...
I must use methods from standard dll im my C#code. In dll declaration of
method is like this:

typedef (__stdcall *ExecuteTaskFromXML)( const string& input, string&
output, DWORD& dwSize, LPVOID& pCtx );

How can I import (and use of course) this method? I try:
[DllImport("ExportDll.dll")]
public static extern void ExecuteTaskFromXML(String input, ref String
output, int dwSize, int pCtx);

but I've got error: System.NullReferenceException. (on String output)

What I must do? Some sample, docs or "entry point in codeproject" ;)) will
be best for me...

Please help

Regards
Krzysztof


Nov 15 '05 #2
Hi,
I must use methods from standard dll im my C#code. In dll declaration of
method is like this:

typedef (__stdcall *ExecuteTaskFromXML)( const string& input, string&
output, DWORD& dwSize, LPVOID& pCtx );

How can I import (and use of course) this method? I try:
[DllImport("ExportDll.dll")]
public static extern void ExecuteTaskFromXML(String input, ref String
output, int dwSize, int pCtx);
If you don't need an output do this

[DllImport(DLLNAME, EntryPoint=PROC_NAME,
CharSet=CharSet.Ansi, ExactSpelling=true,
CallingConvention=CallingConvention.StdCall)]
public static extern bool LoadExample(int anything,
String fileName);

If you need output you need to pass an IntPtr as reference to some
memory reserved by the marshal class.
but I've got error: System.NullReferenceException. (on String output)

What I must do? Some sample, docs or "entry point in codeproject" ;)) will
be best for me...

Please help

Regards
Krzysztof


Nov 15 '05 #3
One more thing - the unmanaged function is declared as "__stdcall". I am not
sure this is the same that the "WINAPI" convention expected by the .NET's
P/Invoke by default.


It is.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Nov 15 '05 #4
> If you need output you need to pass an IntPtr as reference to some
memory reserved by the marshal class.


Some sample?

How to do this...

Regards Krzysztof
Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Michael Murphy | last post by:
I am writing a C# class library that exposes a method that return a structure. I am registering the assembly for COM interop and can reference the assembly in VB (Actually an Acces 2000 Code...
2
by: Beringer | last post by:
Why do I get the following run time error: Additional information: Type System.Object can not be marshaled as an unmanaged structure; no meaningful size or offset can be computed. When the...
0
by: weixian_shen | last post by:
I'm trying to call my DLL written in C, and got the error: Cannot marshal field 'b' of type 'mystruct': There is no marshaling support for this type. The 2 functions in the DLL are: void...
13
by: Just Me | last post by:
The following almost works. The problem is Marshal.PtrToStringAuto seems to terminate at the first null so I don't get the full string. Any suggestions on how to fix this? Or how to improve the...
2
by: jason | last post by:
hello everyone, i have had a suppot ticket open with microsoft for some time to investigate a memory leak issue we are experiencing on our production web servers. the web servers host both ASP...
20
by: Frank Rizzo | last post by:
I have a class that handles events from another class. Unfortunately these events arrive on a different thread. How can I marshal the code execution to the thread on which the caller class was...
10
by: David Fort | last post by:
Hi, I'm upgrading a VB6 app to VB.net and I'm having a problem with a call to a function provided in a DLL. The function takes the address of a structure which it will fill in with values. I...
1
by: sabys | last post by:
I've been noticing a memory leak with the following sample code snippet. Can someone please advise. Have a C# Winforms app with the following code on a button-click event. private void...
1
by: Frankie | last post by:
I have been learning about asynchronous method calls and I keep coming across this term, "marshal" and I would like to know what it means - specifically. AFAIKT, it means "send" but I suspect there...
0
by: Dilip | last post by:
I have cross-posted this question to 3 newsgroups since I am not sure exactly where it belongs (and some NG's have very less activity). Apologies in advance for this. I have set the followup to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.