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

No end of problems trying to use a C++ function....

All, i'm at my wits-end. I have this third party C++ function which I am
trying to call from C#, no matter what I try to do I just can't get it to
work.
This is the function prototype in C++:

long IsValidCompressedFile (
const char *pcszSrcFileName,
BOOL *bValid,
char *origFileName,
long lBufferSize);

This has been (one of) my attempt(s) at accessing it in C#:

[DllImport("external.dll")]
public static extern int IsValidCompressedFile( string srcFileName, ref bool
bValid, ref string origFileName, long lBufferSize );

I've then been using it like so:

....
bool bValid = false;
string strOrigFileName = "";
long validRes = SMSPCIsValidCompressedFile( "c:\new.zip", ref bValid, ref
strOrigFileName, 9999999 );
.....

This particular instance fails with "Object reference not set to an instance
of an object"
Will someone, hell *anyone* please clue me up as to how I can access this
function properly. It's been driving me round the bend.

Thanks in advance.
PT
Nov 16 '05 #1
8 1686

"Paul Tomlinson" <ru************@hotmail.com> wrote in message
news:Of****************@tk2msftngp13.phx.gbl...
All, i'm at my wits-end. I have this third party C++ function which I am
trying to call from C#, no matter what I try to do I just can't get it to
work.
This is the function prototype in C++:

long IsValidCompressedFile (
const char *pcszSrcFileName,
BOOL *bValid,
char *origFileName,
long lBufferSize);

This has been (one of) my attempt(s) at accessing it in C#:

[DllImport("external.dll")]
public static extern int IsValidCompressedFile( string srcFileName, ref
bool bValid, ref string origFileName, long lBufferSize );


I think it's easiest to marshal the char* as a byte[].

[DllImport("external.dll")]
public static extern int IsValidCompressedFile(
string srcFileName,
ref int bValid,
byte[] origFileName,
int lBufferSize)

David
Nov 16 '05 #2


"Paul Tomlinson" <ru************@hotmail.com> wrote in message
news:Of****************@tk2msftngp13.phx.gbl...
All, i'm at my wits-end. I have this third party C++ function which I am
trying to call from C#, no matter what I try to do I just can't get it to
work.
This is the function prototype in C++:

long IsValidCompressedFile (
const char *pcszSrcFileName,
BOOL *bValid,
char *origFileName,
long lBufferSize);

This has been (one of) my attempt(s) at accessing it in C#:

[DllImport("external.dll")]
public static extern int IsValidCompressedFile( string srcFileName, ref
bool bValid, ref string origFileName, long lBufferSize );

I've then been using it like so:

...
bool bValid = false;
string strOrigFileName = "";
long validRes = SMSPCIsValidCompressedFile( "c:\new.zip", ref bValid, ref
strOrigFileName, 9999999 );
....

This particular instance fails with "Object reference not set to an
instance of an object"
Will someone, hell *anyone* please clue me up as to how I can access this
function properly. It's been driving me round the bend.

Thanks in advance.
PT


int are 64 bit in C#!
Why ref for 3rd arg and not fot the first?
Change your declaration into:

public static extern int IsValidCompressedFile( ref StringBuilder
srcFileName, ref bool
bValid, ref StringBuilder origFileName, int lBufferSize );

Also make sure API is expecting ANSI strings, and the calling sequence
matches the one used by PInvoke (_stdcall).

Willy.
Nov 16 '05 #3
"Willy Denoyette [MVP]" wrote:


"Paul Tomlinson" <ru************@hotmail.com> wrote in message
news:Of****************@tk2msftngp13.phx.gbl...
All, i'm at my wits-end. I have this third party C++ function which I am
trying to call from C#, no matter what I try to do I just can't get it to
work.
This is the function prototype in C++:

long IsValidCompressedFile (
const char *pcszSrcFileName,
BOOL *bValid,
char *origFileName,
long lBufferSize);

This has been (one of) my attempt(s) at accessing it in C#:

[DllImport("external.dll")]
public static extern int IsValidCompressedFile( string srcFileName, ref
bool bValid, ref string origFileName, long lBufferSize );

I've then been using it like so:

...
bool bValid = false;
string strOrigFileName = "";
long validRes = SMSPCIsValidCompressedFile( "c:\new.zip", ref bValid, ref
strOrigFileName, 9999999 );
....

This particular instance fails with "Object reference not set to an
instance of an object"
Will someone, hell *anyone* please clue me up as to how I can access this
function properly. It's been driving me round the bend.

Thanks in advance.
PT

int are 64 bit in C#!
Why ref for 3rd arg and not fot the first?
Change your declaration into:

public static extern int IsValidCompressedFile( ref StringBuilder
srcFileName, ref bool
bValid, ref StringBuilder origFileName, int lBufferSize );

Also make sure API is expecting ANSI strings, and the calling sequence
matches the one used by PInvoke (_stdcall).


you need to drop the ref on stringbuilder because that's going to be an
additional level of indirection. and the first argument can stay as string
since it's not modified.

and ints are not 64bit in C#, but longs in C are 32bit which maps to int
(Int32).

and I think BOOL* needs to be marshalled as "ref int"

public static extern int IsValidCompressedFile(
string pcszSrcFileName,
ref int bValid,
StringBuilder origFileName,
int lBufferSize );

make sure when you call it, allocate the buffer for the stringbuilder.

Willy.

Nov 16 '05 #4
> int are 64 bit in C#!
Why ref for 3rd arg and not fot the first?
Change your declaration into:

public static extern int IsValidCompressedFile( ref StringBuilder
srcFileName, ref bool
bValid, ref StringBuilder origFileName, int lBufferSize );

Also make sure API is expecting ANSI strings, and the calling sequence
matches the one used by PInvoke (_stdcall).

Willy.


What and then call it like:

StringBuilder sourcefile = new StringBuilder( @"c:\test.zip" );
StringBuilder destfile = new StringBuilder();
long validRes = IsValidCompressedFile( ref sourcefile, ref bValid, ref
destfile, 9999 );

:-<
"Capacity exceeds maximum capacity. Parameter name: capacity"

Nov 16 '05 #5
Thanks for everyones help so-far. While we are on topic what would be the
C# equivilent of this C++ prototype?

long DecompressFile (
const char *pcszSrcFileName,
const char *pcszDstPath,
PFN pfnCallback,
long lCallback);

PFN is defined as:
long PFN (
long lCallback,
unsigned short type,
long progress);

I don't actually need to specify any of the PFN struct attributes but do I
still need to define it in C#?

Thanks again :-/
Nov 16 '05 #6
I think you need to wrap that with an extern "C" declaration, as
follows:

long IsValidCompressedFile (
const char *pcszSrcFileName,
BOOL *bValid,
char *origFileName,
long lBufferSize);

extern "C" {
long IsValidCompressedFile ( const char *pcszSrcFileName, BOOL
*bValid, char *origFileName, long lBufferSize);
}

The reason is that the name of that function in the DLL will be mangled
according to C++ conventions and these vary by the compiler vendor. By
wrapping them in extern "C" declarations, you force the system to
generate a DLL signature that looks like what it has.

You can see this by doing the following:

1. Launch your Visual Studio command prompt.
2. dumpbin /exports yourdll
3. dumpbin /exports c:\windows\system32\gdi32.dll.

Compare the way the signatures look. In yourdll, if you see a bunch of
funny characters in your name, that's how you know you are compiling to
C++ function signatures and not C.
This has been (one of) my attempt(s) at accessing it in C#:

[DllImport("external.dll")]
public static extern int IsValidCompressedFile( string srcFileName, ref bool bValid, ref string origFileName, long lBufferSize );

This seems like this should be correct.
...
bool bValid = false;
string strOrigFileName = "";
long validRes = SMSPCIsValidCompressedFile( "c:\new.zip", ref bValid, ref strOrigFileName, 9999999 ); This particular instance fails with "Object reference not set to an instance of an object"
This is because C#'s runtime cannot locate the function within the DLL,
therefor, SMSCPCxxx is not a valid object to call.
Will someone, hell *anyone* please clue me up as to how I can access this function properly. It's been driving me round the bend.

Thanks in advance.


Good luck!

Nov 16 '05 #7
Daniel,

Inline ***

Willy.

"Daniel Jin" <Da*******@discussions.microsoft.com> wrote in message
news:64**********************************@microsof t.com...
"Willy Denoyette [MVP]" wrote:
int are 64 bit in C#!
Why ref for 3rd arg and not fot the first?
Change your declaration into:

public static extern int IsValidCompressedFile( ref StringBuilder
srcFileName, ref bool
bValid, ref StringBuilder origFileName, int lBufferSize );

Also make sure API is expecting ANSI strings, and the calling sequence
matches the one used by PInvoke (_stdcall).


you need to drop the ref on stringbuilder because that's going to be an
additional level of indirection. and the first argument can stay as
string
since it's not modified.

*** My bad, added a ref to the first arg instead of to remove ref from the
3rd.
and ints are not 64bit in C#, but longs in C are 32bit which maps to int
(Int32).
*** Sorry long is what I meant.
and I think BOOL* needs to be marshalled as "ref int"
*** No BOOL* correctly marshals as ref bool, but sure you can use "ref int"
as well.
public static extern int IsValidCompressedFile(
string pcszSrcFileName,
ref int bValid,
StringBuilder origFileName,
int lBufferSize );

make sure when you call it, allocate the buffer for the stringbuilder.

Willy.

Nov 16 '05 #8
PFN is not a structure, but a method call. Your DecompressFile expects
you to pass a pointer to a function to it. There is a code sample on
how to do this sort of thing here:
http://www.codeproject.com/csharp/windowhider.asp

Nov 16 '05 #9

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

Similar topics

0
by: Jerome Lefebvre | last post by:
Hello, Hope this will interest a few. I been working with a friend on the problems given out during the "International Collegiate Programming Contest" (ICPC) http://icpc.baylor.edu/icpc/ ....
2
by: Justin Lemkul | last post by:
Hello all, I am hoping someone out there will be able to help me. I am trying to install a program that utilizes NumPy. In installing NumPy, I realized that I was lacking Atlas. I ran into...
16
by: Honestmath | last post by:
Hi, I added the following line to my code within a class declaration: std::vector<Date> m_duedates(100); I also tried: std::vector<Date> m_duedates(100, Date());
3
by: Mark Morton | last post by:
I'm writing an if statement for a UK credit card form validation script. Users who specify that their card is Switch need to enter either the issue number or the 'valid from' date. I'm trying to...
9
by: robbie.carlton | last post by:
Hello! I've programmed in c a bit, but nothing very complicated. I've just come back to it after a long sojourn in the lands of functional programming and am completely stumped on a very simple...
21
by: matvdl | last post by:
I have a system that was originally developed in asp - the pages are saved in SQL (there are over 10,000 pages) and saved to a temp directory in the server when requested by a client. I have...
3
by: Ryan Riehle | last post by:
Hi All! Trying to upgrade to Apache 2.0.49 and getting compile errors related to mod_auth_pgsql, any clue?: make: Entering directory `/usr/src/httpd-2.0.49'...
2
by: Mike | last post by:
Hi, I am new to C and having problems with the following program. Basically I am trying to read some files, loading data structures into memory for latter searching. I am trying to use structres...
10
by: Cliff | last post by:
Greetings, I have been trying to teach myself C++ over the past few weeks and have finally came across a problem I could not fix. I made a simple program that prints out a square or rectangle...
3
by: John Dann | last post by:
Trying to learn Python here, but getting tangled up with variable scope across functions, modules etc and associated problems. Can anyone advise please? Learning project is a GUI-based...
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...
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
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...
0
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...

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.