473,388 Members | 1,493 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,388 software developers and data experts.

P/Invoke and return char*

Hi,

I have software which have to read data using dll writen in C. This
library has method which has header like this:

int fnGetDescription(int allwaysOpen, char *itemNumber, char *desc, char
*price, char *priceUnit, char *tempName, char *user1, char *user2, char
*user3);

allwaysOpean is 0 for every call. itemNumber is article code and other
parameters are returned data about article with itemCode (article
description, article price, etc.).

I wrote application in Borlan C++ and everything work fine. Below is
sample app in BC:

typedef int (*fnGetDescription)(int allwaysOpen, char *itemNumber, char
*desc, char *price, char *priceUnit, char *tempName, char *user1, char
*user2, char *user3);
typedef int (*fnOpenConnect)(char *sMsgQueue, char *sFileName);
typedef int (*fnReadConnfiguration)(char *workDir);

void __fastcall TForm1::Button1Click(TObject *Sender)
{
HINSTANCE hInst;
fnGetDescription get;
fnOpenConnect open;
fnReadConnfiguration read;
int odp;
char sDesc[170] = { 0 };
char sPrice[90] = { 0 };
char sPricePerUnit[90] = { 0 };
char sTemplateName[90] = { 0 };
char sUser1[200] = { 0 };
char sUser2[200] = { 0 };
char sUser3[200] = { 0 };

hInst = LoadLibrary("Iss400.dll");
if(hInst != NULL)
{
read = (fnReadConnfiguration)GetProcAddress(hInst,
"fnReadConnfiguration");
open = (fnOpenConnect)GetProcAddress(hInst, "fnOpenConnect");
get = (fnGetDescription)GetProcAddress(hInst, "fnGetDescription");

if(read != NULL)
{
odp = read("c:\\PriceAnyWhere\\");
if(odp == 0)
{
if(open !=NULL)
{
odp = open("ADXLXCCN",
"C:\\PriceAnywhere\\towary.txt");

if(odp == 0)
{
if(get != NULL)
{
odp = get(0, "some code to check", sDesc,
sPrice, sPricePerUnit, sTemplateName, sUser1, sUser2, sUser3);

if(odp == 0)
{

// In this point I get properly data. I mean in parameter sUser1 I
get some string (not empty string).

}
....
Now I need to call this methods in the same way from C# code. I prepare
dllimports like this:

[DllImport(@"C:\PriceAnyWhere\Iss400.dll", CharSet = CharSet.Ansi)]
public static extern int fnReadConnfiguration(string WorkDirectory);

[DllImport(@"C:\PriceAnyWhere\Iss400.dll", CharSet = CharSet.Ansi)]
public static extern int fnOpenConnect(string Queue, string FileName);

[DllImport(@"C:\PriceAnyWhere\Iss400.dll", CharSet = CharSet.Ansi)]
public static extern int fnGetDescription(int openAlways, string EAN,
System.Text.StringBuilder desc, System.Text.StringBuilder price,
System.Text.StringBuilder unitPrice, System.Text.StringBuilder tempName,
[MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder user1,
System.Text.StringBuilder user2, System.Text.StringBuilder user3);

And call like this:

System.Text.StringBuilder sDesc = new System.Text.StringBuilder("",170);
System.Text.StringBuilder sPrice = new System.Text.StringBuilder("",90);
System.Text.StringBuilder sPricePerUnit = new
System.Text.StringBuilder("", 90);
System.Text.StringBuilder sTemplateName = new
System.Text.StringBuilder("", 90);
System.Text.StringBuilder sUser1 = new System.Text.StringBuilder("", 200);
System.Text.StringBuilder sUser2 = new System.Text.StringBuilder("", 200);
System.Text.StringBuilder sUser3 = new System.Text.StringBuilder("", 200);
string resp = "";

if(fnReadConnfiguration(System.IO.Path.GetDirector yName(@"C:\PriceAnyWhere\"))==0)
{
if(fnOpenConnect("ADXLXCCN", @"C:\PriceAnyWhere\towary.txt")==0)
{
if(fnGetDescription(0, "the same code like in BC code", sDesc, sPrice,
sPricePerUnit, sTemplateName, sUser1, sUser2, sUser3)==0)
{
// Point A - Comment below
}
}
}

In point A I get data but not all. parameters sDesc and SPrice are
returned correctly but sUser1 not. I get empty string. The others
parameters are not impotant for me.

What can be wrong? What can I change to get sUser1 parameter?
--
Best regards,
Klaudiusz Bryja
Nov 7 '06 #1
0 2588

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

Similar topics

4
by: C# Learner | last post by:
How can I "invoke" a method containing parameters? I've tried creating my own delegates and using them to do it, but can't get it working. Can't find any info on this anywhere. e.g.: public...
13
by: Christian Westerlund | last post by:
Hi! I'm trying to use P/Invoke and a Method which takes an IntPtr where I am supposed to put an address to a method which the native method will use to communicate back to me. How do I convert a...
16
by: Duncan Mole | last post by:
Hi, This is probably an easy one but it iy first bit of p/invoke. I am trying to use the following C struct in a call: typedef struct { BYTE SRB_Cmd; BYTE SRB_Status, BYTE ...
2
by: russb_69 | last post by:
Russ here. I'm new to this forum. I need help in understanding why the following code does not work. Basically, I'm defining a structure in a c# application, and using a c++ dll to modify it. ...
14
by: stic | last post by:
Hi, I'm in a middle of writing something like 'exception handler wraper' for a set of different methodes. The case is that I have ca. 40 methods form web servicem, with different return values...
1
by: MarkOwen | last post by:
I am exploring using P/Invoke to call some legacy code from our customer contact software. I have been trying some simple test scenarios and haven't had success. Can someone point me to an example...
7
by: stephan querengaesser | last post by:
hi ng, i try to invoke a webservice-method with an filter-object, that contains value types. if i donīt want to filter the return value of the method, i have to pass a new instance of the...
0
by: truthbajaj | last post by:
Hi, I am using Oracle 9i and Unix on my system and trying to execute a UNIX shell command through external procedure in C. I created a shared lib (libextproc.so) for the following function. ...
6
inch
by: inch | last post by:
Hello all.. I'm new to C# and I've not actually started serious coding. However I know I will need to call some legacy functions from a company DLL. I have tested this using P/Invoke and been quite...
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: 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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.