473,503 Members | 1,831 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

marshalling c style command line arguments

I am wrapping a legacy C api using C++\CLI. I will use the resulting classes
from C# and VB.net. I need to pass command line arguments from the .net
languages to the c api functions, so I've created a custom marshaller for the
task. The problem is that I get an error stating that a conversion from
type1 to type2 is not supported. I've read the MSDN documentation (along
with an apress title or two), but I am still missing somthing.

Here is my code
VS.NET 2008:

The custom marshaler:
#pragma once

#include <msclr/marshal.h>

namespace msclr {
namespace interop {
template<>
ref class context_node<unsigned char **, System::String ^>
: public context_node_base
{
private:
unsigned char **toPtr;
int from_count;

public:
context_node(unsigned char **& toObject, array<System::String^>^
fromObject)
{
// (Step 4) Initialize toPtr to the appropriate empty value.
from_count = fromObject->GetLength(0);

// (Step 5) Insert conversion logic here.
toPtr =(unsigned char**) new unsigned char[from_count];
for(int i=0;i<from_count;i++)
{
cli::array<unsigned char>^ char_array =
System::Text::Encoding::ASCII->GetBytes(fromObject[i]);
cli::pin_ptr<unsigned charchar_pin_ptr = &char_array[0];
unsigned char* char_star = new unsigned char[char_array->GetLength(0)];
toPtr[i] = char_star;
}

// (Step 6) Set toObject to the converted parameter.
toObject = toPtr;
}
~context_node()
{
this->!context_node();
}
protected:
!context_node()
{
// (Step 7) Clean up native resources.
for(int i=0;i<from_count;i++)
{
delete [] toPtr[i];
}
delete [] toPtr;
}
};
}
}

The wrapper class:

public ref class Session
{
public:
//array<System::String^ >^ args = string[] args
Session(array<System::String^ >^ args)
{
Console::WriteLine(args[1]);
marshal_context context;// = gcnew marshal_context();
unsigned char **argv;
argv = context.marshal_as<unsigned char**>(args);
//STATUS error = NotesInitExtended (args, argv);

//if (error)
//{
// fprintf (stderr, "\nError initializing Notes.\n");
// throw std::exception("Error initializing Notes");
// //return (1);
//}
}

~Session()
{
NotesTerm();
}
};
Nov 3 '08 #1
2 2677
Hello Giovanni,

Thank you very much for your reply. This should be just what I need to
better understand custom marshalers in this context.

I am stuck with using char ** since this is what the 3rd party api is
expecting. I will have to convert the strings from Unicode to ANSI.

I will try this out tonight.

Thanks again.

Kelvin

"Giovanni Dicanio" wrote:
>
"Kelvin Johnson" <ke************@discussions.microsoft.comha scritto nel
messaggio news:2F**********************************@microsof t.com...
I am wrapping a legacy C api using C++\CLI. I will use the resulting
classes
from C# and VB.net. I need to pass command line arguments from the .net
languages to the c api functions, so I've created a custom marshaller for
the
task.

I prepared and attached a compilable code for doing that.

You can see the main() function for how to do the marshaling (I won't repeat
that here), using the class I provided.

However, note that there may be some problems in your requirements, e.g. C#
strings are Unicode UTF-16 strings, instead it seems to me that you want to
marshal to char* strings (which are ANSI/MBCS).
I would prefer a marshaling to native Unicode strings (wchar_t *).

In case you would like to work on a higher level, and use std::vector for
string container instead of raw C-like char** pointers, you may want to use
a custom marshaler I wrote and shared on CodeGallery (it uses Unicode
strings in form of std::wstring and std::vector as string container):

http://code.msdn.microsoft.com/MarshalAsStringArray

HTH,
Giovanni
Nov 4 '08 #2

"Kelvin Johnson" <ke************@discussions.microsoft.comha scritto nel
messaggio news:5F**********************************@microsof t.com...
Thank you very much for your reply.
You're welcome.
I am stuck with using char ** since this is what the 3rd party api is
expecting. I will have to convert the strings from Unicode to ANSI.

I will try this out tonight.
OK, if you have any problem let us know.

Note that sometimes when the char ** are involved, the end of the string
pointer array is marked with a NULL pointer; in the code I posted, I did not
do that, but the marshaling class has a Count() method to get the number of
strings in the array.
However, it is trivial to modify the code in case you want a NULL pointer
termination for the array.

Giovanni
Nov 4 '08 #3

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

Similar topics

6
2589
by: Hari | last post by:
can i have command line arguments in VS.NET applicatio? if yes how? Can i have some code snippets of the above functionality? I know we can acjieve this in console application form command...
6
628
by: Jon Hewer | last post by:
hi i am writing a little script and currently implementing command line arguments following the guide by mark pilgrim from dive into python; ...
7
4715
by: Steve M | last post by:
I'm trying to invoke a Java command-line program from my Python program on Windows XP. I cannot get the paths in one of the arguments to work right. The instructions for the program describe the...
2
4167
by: SunRise | last post by:
Hi I am creating a C Program , to extract only-Printable-characters from a file ( any type of file) and display them. OS: Windows-XP Ple help me to fix the Errors & Warnings and explain...
1
7878
by: amirmira | last post by:
I would like to set command line arguments to a service at install time. I need to do this because I need to get information from different registry locations depending on my command line argument....
1
2436
by: Rune Jacobsen | last post by:
Hi, I've been trying to figure this one out, but my experience just doesn't have what it takes... :| I am writing an application that reads an XML file and displays the contents in various...
40
2686
by: raphfrk | last post by:
I have a program which reads in 3 filenames from the command line prog filename1 filename2 filename3 However, it doesn't work when one of the filenames has spaces in it (due to a directory...
2
2741
by: Milan | last post by:
Hi, Please guide me how to set command line argument and how to retrive command line argument. Senario: vb.net application should be able to execute from command prompt by passing login and...
7
9332
by: Jwe | last post by:
Hi, I've written a program which has both a command line interface and Windows form interface, however it isn't quite working correctly. When run from command line with no arguments it should...
0
7199
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
7076
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7323
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...
0
7453
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5576
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5005
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3151
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1507
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
732
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.