473,396 Members | 2,018 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,396 software developers and data experts.

pass char* over dll-boundaries

my question is as follows:
I've got a DLL in which I have a method GetBuffer (this one is extern,
exported, is called from outside this program) which shall pass a
char-buffer to the calling-function for further handling.

If I've confused you, here's the code snippet (it's within a simple
Win32-Dll-project):

char* buffer; // global buffer - this one points to my buffer (for
puttiing it simple in here I assume memory is already allocated and buffer
points to the correct string)
int bufferLen; // global bufferlength - value assigned beforehand,
too

extern "C" __declspec(dllexport)
void GetBuffer(int *length, char retBuffer[]) // out-paramaters (buffer
shall be returned)
{
*length = bufferLen;
memcpy(retBuffer, buffer, bufferLen);
}
----------------------------------------------------------------------------
-
I call this function and want to make use of the buffer within a simple
win32-c++-console-application like this:

extern "C" __declspec(dllimport) void GetBuffer(int *length, char
retBuffer[]);

int _tmain(int argc, _TCHAR* argv[])
{
char retBuffer[1000];
int length;
GetBuffer(&length, retBuffer); // call exported function
printf("Buffer: %s\n", retBuffer);
}
---------------------------------------------------------------------------

Still confused? Ok: I just want to get the char*-buffer into my win32-app
that uses this dll - so I just want to ask what I have to do that I can
"export" the char* buffer? I'm not sure if I can use a char*-parameter in
the GetBuffer-function or if I am to use a char[]-array as tried above.
-----
Fact is, that if I execute the above code, I get a memory-access-error - so
I must have made a crucial mistake.

I appreciate your hints,
ekim!
Jul 22 '05 #1
8 6142

"Ekim" <th************@gmx.net> wrote in message
news:2p************@uni-berlin.de...
my question is as follows:
I've got a DLL in which I have a method GetBuffer (this one is extern,
exported, is called from outside this program) which shall pass a
char-buffer to the calling-function for further handling.


[snip]

Passing memory from an executable to a DLL is not something that is defined
by the C++ language, so this question is off topic here. But I think you
will find that the problem is that memory allocated in one DLL or executable
cannot be access in another DLL or executable. So that is the problem, not
the way that you have declared the method or the buffer.

For details on how to solve this problem please ask in a topical newsgroup,
news:comp.os.ms-windows.programmer.win32 for instance.

John
Jul 22 '05 #2
"John Harrison" <jo*************@hotmail.com> wrote in message
news:2p************@uni-berlin.de...

"Ekim" <th************@gmx.net> wrote in message
news:2p************@uni-berlin.de...
my question is as follows:
I've got a DLL in which I have a method GetBuffer (this one is extern,
exported, is called from outside this program) which shall pass a
char-buffer to the calling-function for further handling.

[snip]

Passing memory from an executable to a DLL is not something that is

defined by the C++ language, so this question is off topic here. But I think you
will find that the problem is that memory allocated in one DLL or executable cannot be access in another DLL or executable. So that is the problem, not
the way that you have declared the method or the buffer.

For details on how to solve this problem please ask in a topical newsgroup, news:comp.os.ms-windows.programmer.win32 for instance.

John


Good, my question is not really about DLL-boundaries, but about
buffer-passing over functions in c++ in general.

So just a simple example:
----------------
static char* tmpString = NULL;
static int tmpLength = -1;

void MyFunction(char* inputString, int inputSize) // here I get the
correct buffer
{
tmpString = inputString; // copy buffer into global variable
tmpLength = inputSize;
}

// now I want to print the buffer in any method like this:
void AnyMethod()
{
printf("Buffer: %s\n", tmpString);
}

I guess the above code works. But is there for example a possibility to
transform the char* "inputString" I got in MyFunction into a byte-array of
the length "inputSize" I got in MyFunction?

thx
Jul 22 '05 #3
> void MyFunction(char* inputString, int inputSize) // here I get the
correct buffer
{
tmpString = inputString; // copy buffer into global variable
tmpLength = inputSize;
}

// now I want to print the buffer in any method like this:
void AnyMethod()
{
printf("Buffer: %s\n", tmpString);
}
I don't see any problem if you do this :
void AnyMethod()
{
char tmpString[_BUFFERSIZE];
MyFunction(inputString, BUFFERSIZE);
printf("Buffer: %s\n", tmpString);

}
Just pay attention to : when you handle a pointer to a memory block to other
routine, if you
accidently delete it in the callee, the caller will have a dangling pointer.

John.

"Ekim" <th************@gmx.net> wrote in message
news:2p************@uni-berlin.de... "John Harrison" <jo*************@hotmail.com> wrote in message
news:2p************@uni-berlin.de...

"Ekim" <th************@gmx.net> wrote in message
news:2p************@uni-berlin.de...
my question is as follows:
I've got a DLL in which I have a method GetBuffer (this one is extern,
exported, is called from outside this program) which shall pass a
char-buffer to the calling-function for further handling.


[snip]

Passing memory from an executable to a DLL is not something that is

defined
by the C++ language, so this question is off topic here. But I think you
will find that the problem is that memory allocated in one DLL or

executable
cannot be access in another DLL or executable. So that is the problem, not the way that you have declared the method or the buffer.

For details on how to solve this problem please ask in a topical

newsgroup,
news:comp.os.ms-windows.programmer.win32 for instance.

John


Good, my question is not really about DLL-boundaries, but about
buffer-passing over functions in c++ in general.

So just a simple example:
----------------
static char* tmpString = NULL;
static int tmpLength = -1;

void MyFunction(char* inputString, int inputSize) // here I get the
correct buffer
{
tmpString = inputString; // copy buffer into global variable
tmpLength = inputSize;
}

// now I want to print the buffer in any method like this:
void AnyMethod()
{
printf("Buffer: %s\n", tmpString);
}

I guess the above code works. But is there for example a possibility to
transform the char* "inputString" I got in MyFunction into a byte-array of
the length "inputSize" I got in MyFunction?

thx

Jul 22 '05 #4

"Ekim" <th************@gmx.net> wrote in message
news:2p************@uni-berlin.de...
"John Harrison" <jo*************@hotmail.com> wrote in message
news:2p************@uni-berlin.de...

"Ekim" <th************@gmx.net> wrote in message
news:2p************@uni-berlin.de...
my question is as follows:
I've got a DLL in which I have a method GetBuffer (this one is extern,
exported, is called from outside this program) which shall pass a
char-buffer to the calling-function for further handling.

[snip]

Passing memory from an executable to a DLL is not something that is

defined
by the C++ language, so this question is off topic here. But I think you
will find that the problem is that memory allocated in one DLL or

executable
cannot be access in another DLL or executable. So that is the problem, not the way that you have declared the method or the buffer.

For details on how to solve this problem please ask in a topical

newsgroup,
news:comp.os.ms-windows.programmer.win32 for instance.

John


Good, my question is not really about DLL-boundaries, but about
buffer-passing over functions in c++ in general.


Well, your original question *was* about passing between different
applications, which is *ver* different from just passing between functions
in the same application.
So just a simple example:
----------------
static char* tmpString = NULL;
static int tmpLength = -1;

void MyFunction(char* inputString, int inputSize) // here I get the
correct buffer
{
tmpString = inputString; // copy buffer into global variable
This does *not* copy the buffer, it merely makes tmpString point to the same
character(s) as inputString points to. If inputString is deleted, then
tmpString will become an invalid pointer.
tmpLength = inputSize;
}

// now I want to print the buffer in any method like this:
void AnyMethod()
{
printf("Buffer: %s\n", tmpString);
}

I guess the above code works. But is there for example a possibility to
transform the char* "inputString" I got in MyFunction into a byte-array of
the length "inputSize" I got in MyFunction?


What's to transform? You can use a char* pointer in the same fashion as an
array variable. If you need a *separate* char array, then use new
char[size] (and later delete[]) to instantiate a char* pointer (to an array
of char), and strcpy the contents from one string to the other.

But you'll probably save yoursefl a lot of effort if you start using the
std::string class for most string handling.

(So what happened to your original problem of passing the string from a
DLL?)

-Howard

Jul 22 '05 #5

"John Sun" <JS***********@gmail.com> skrev i en meddelelse
news:41***********************@news.twtelecom.net. ..
void MyFunction(char* inputString, int inputSize) // here I get the correct buffer
{
tmpString = inputString; // copy buffer into global variable
tmpLength = inputSize;
}

// now I want to print the buffer in any method like this:
void AnyMethod()
{
printf("Buffer: %s\n", tmpString);
}
I don't see any problem if you do this :
void AnyMethod()
{
char tmpString[_BUFFERSIZE];


But there is a problem here: _BUFFERSIZE is reserved for the implementation.

MyFunction(inputString, BUFFERSIZE);
printf("Buffer: %s\n", tmpString);

}
Just pay attention to : when you handle a pointer to a memory block to other routine, if you
accidently delete it in the callee, the caller will have a dangling

pointer.

Always encapsulta these whenever you can

Peter
Jul 22 '05 #6

" >

Well, your original question *was* about passing between different
applications, which is *ver* different from just passing between functions
in the same application.


Typo: should have been "*very* different", not "*ver* different".

-Howard
Jul 22 '05 #7
> (So what happened to your original problem of passing the string from a
DLL?)


ok ok, to reveal the whole story:
I'm using a pdf-2-text-converter-function from an external vendor which
resides within a dll. This dll forces me to write a callback-function, which
is called when a pdf-file is to be converted. Within this callback-function,
I get a char* that points to the buffer containing the result of the
pdf-converting. The callback function looks like this:

void ContentCallBackFunc(char* i_szStr, int i_size)
{ myBuf = i_szStr; // myBuf is global char*-variable
mySize = i_size; // mySize is global int-variable
}

Now I'm about to write a wrapper around this function (it's again a dll), so
that I can pass in the filename to the pdf-file and get the buffer
containing the converting data back. Let's say a function called
Convert(...) looks something like this:
extern "C" __declspec(dllexport)
void Convert(const char* filename, char **buffer, int *length)
{
convertPdf(filename, (CALLBACK)ContentCallBackFunc); // make use
of the convert-Pdf-function of the external dll --> this implies a call to
ContentCallBackFunc

*buffer = myBuf; // specify output-params
*length = mySize;
}

In any win32-client-program I can use now this external
Convert(...)-function and I shall be able to access the converted buffer. I
make use in a win32-console-app like this:

extern "C" __declspec(dllimport) void Convert(const char* filename, char
**buffer, int *length);

int _tmain(int argc, _TCHAR* argv[])
{
char* buffer = NULL;
int length = -1;
Convert("C:\\test.pdf", &buffer, &length);
printf("Buffer:\t%d\t%s\n", length, buffer); // results in an
memory-access-error!
}

The real question over dll-boundaries is now, whether it is possible to
access the original-buffer from the callback-function just by redirecting
some pointers, or if I have to copy the original pointer to a buffer into a
second buffer-array to be able to access it from another application!?
The key is that I want to avoid copying buffers whenever it is possible,
because my application is very time-critical and eventually it should call
that converting-function thousands of times within a short period. So I
really would prefer just redirecting pointers instead of copying buffers.

So this is true story and real background to my initial question.
Would be fine to receive any hints,
ekim!
Jul 22 '05 #8
"Ekim" <th************@gmx.net> wrote in message news:<2p************@uni-berlin.de>...
[snip]
If I've confused you, here's the code snippet (it's within a simple
Win32-Dll-project): [snip] I appreciate your hints,


Probably you should ask this in one of the microsoft.public.vc.*
or one of the msdos or win32 newsgroups.
Socks
Jul 22 '05 #9

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

Similar topics

2
by: Maloney | last post by:
I'm trying to pass a structer from C# to a C dll in .Net Compact Framework This partially works. If in the GetStatus I only set -- status->fileLength = 10; -- and not the second variable then...
1
by: wbaccay | last post by:
I have a byte of binary data received from a NetworkStream (C# code) that I need to pass to the IWMWriter object in a DLL written in Managed extensions for C++ (since the Windows Media SDK is not...
16
by: Ekim | last post by:
hello, I'm allocating a byte-Array in C# with byte byteArray = new byte; Now I want to pass this byte-Array to a managed C++-function by reference, so that I'm able to change the content of the...
2
by: John Smith | last post by:
Hello :) I have a DLL file written in C where one function accepts an array of structures as input paramter and pointer to length integer. This function will fill up the array and specify the...
2
by: 知易行难 | last post by:
sorry i am poor in English. i am trying to call functions from a "dll"(maybe developed in C++), there is a struct in this function,which is defined like this: typedef struct{ .......... BYTE *...
7
by: SB | last post by:
What is the proper way to pass a character array (char *) from a "C" dll to a C# method (delegate) in my app? Getting the dll (which simulates a third party dll) to call my delegate works fine. ...
0
by: Owen Corpening | last post by:
I have this c# interface: int EditActuateConfigFile( string s); and I call it from C++ with a char * - it compiles, at runtime there is no complaint, but I put logging in the method and it isn't...
5
by: Cc | last post by:
hi, how do I pass fixed string byref to a dll . I had try <VBFixedString(10)> but when I run I keep getting system.accessviolation Any help would be great
5
by: Stephen Cawood | last post by:
I'm trying to use a C++ .lib from C# (I tried the Interop group will no results). I have a working wrapper DLL (I can get back simple things like int), but I'm having issues dealing with an array...
29
by: Kenzogio | last post by:
Hi, I have a struct "allmsg" and him member : unsigned char card_number; //16 allmsg.card_number
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: 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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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
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,...

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.