473,656 Members | 2,824 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<un signed char **, System::String ^>
: public context_node_ba se
{
private:
unsigned char **toPtr;
int from_count;

public:
context_node(un signed char **& toObject, array<System::S tring^>^
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_coun t;i++)
{
cli::array<unsi gned char>^ char_array =
System::Text::E ncoding::ASCII->GetBytes(fromO bject[i]);
cli::pin_ptr<un signed charchar_pin_pt r = &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_coun t;i++)
{
delete [] toPtr[i];
}
delete [] toPtr;
}
};
}
}

The wrapper class:

public ref class Session
{
public:
//array<System::S tring^ >^ args = string[] args
Session(array<S ystem::String^ >^ args)
{
Console::WriteL ine(args[1]);
marshal_context context;// = gcnew marshal_context ();
unsigned char **argv;
argv = context.marshal _as<unsigned char**>(args);
//STATUS error = NotesInitExtend ed (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 2683
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.mi crosoft.comha scritto nel
messaggio news:2F******** *************** ***********@mic rosoft.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.mi crosoft.comha scritto nel
messaggio news:5F******** *************** ***********@mic rosoft.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
2602
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 prompt but how can i achieve it in an application in VS.NET? Thank you
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; http://diveintopython.org/scripts_and_streams/command_line_arguments.html thats all fine, however i am not sure of the BEST way to handle multiple command line arguments
7
4729
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 following for the command-line arguments: java -jar sforcedataloader.jar -Dsalesforce.config.dir=CONFIG_DIRECTORY They also give an example:
2
4186
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 how to use Command-Line Arguments inside C program.
1
7893
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. I have to do it this way as the consumer of the service should not be able to change the argument - except by uninstalling and reinstalling the service. I created the service and the service itself works great. However, when I try to install...
1
2446
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 ways to the end user. This works fine. My challenge lies in the fact that these XML files are generated by various (third party) applications. Which application generates them depends on the user, the country they are in, their personal preferences,...
40
2728
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 name with a space in it) because that filename gets split into 2. I tried
2
2748
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 password and should be able to execute process form (with his parameter) User should not be able to see GUI
7
9355
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 display the Windows form. The form is being displayed but the command only returns when the form is closed. I want the command line to return immediately, leaving the form displayed.
0
8297
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8717
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8498
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8600
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7311
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6162
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5629
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1930
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.