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

C#-APP: problem with DllImport when string not the first param

I have a simple Win32 DLL I wrote named "SimpleLib" that exports two functions. It is written in C++ and compiled with __stdcall (/Gz) and with the preprocessor definition _MBCS (not Unicode).

The first function is called "StrFirst" and takes in a LPTSTR as the first parameter and a long as the second, like this:
Expand|Select|Wrap|Line Numbers
  1. SIMPLELIB_API int StrFirst(LPTSTR str, long num);
The second function is essentially the same thing, but with the order of the parameters switched. It is called "LongFirst" and takes in a long as the first param and a LPTSTR as the second param, like this:
Expand|Select|Wrap|Line Numbers
  1. SIMPLELIB_API int LongFirst(long num, LPTSTR str);
The implementation inside these functions simply displays the values passed in. Nothing clever.

I also have a Win32 Console Application that calls these functions in the DLL, like this:
Expand|Select|Wrap|Line Numbers
  1. int _tmain(int argc, _TCHAR* argv[])
  2. {
  3.    LPTSTR MyStr = _T("Calling from TestApp");
  4.    long MyNum = 1234;
  5.    int RetVal = 1;
  6.    RetVal = StrFirst(MyStr, MyNum);
  7.    RetVal = LongFirst(MyNum, MyStr);
  8.    return 0;
  9. }
Everything so far works fine. The DLL sees the parameters in both functions without problem.

Now for the fun part! I also have a C# application that calls these two functions in the DLL. Calling the function that has the String as the first argument works fine. But calling the function with the String as the second argument fails. The DLL sees this second parameter as a null argument (not just an empty string, but actually as a zero value). Any idea why this function fails, while the other succeeds?

Here's the source:

Expand|Select|Wrap|Line Numbers
  1. using System.Runtime.InteropServices;   // DllImport
  2. namespace TestApp.Net
  3. {
  4.     class Program
  5.     {
  6.         static void Main(string[] args)
  7.         {
  8.             string str = "Calling from TestApp.Net";
  9.             long num = 1234;
  10.             int RetVal = 1;
  11.  
  12.             // The following call succeeds:
  13.             RetVal = StrFirst(str, num);
  14.  
  15.             // But this fails. The 2nd arg appears in the DLL as a NULL. Why?
  16.             RetVal = LongFirst(num, str);
  17.         }
  18.  
  19.         [DllImport("SimpleLib.dll")]
  20.         public static extern int StrFirst(string str, long num);
  21.  
  22.         [DllImport("SimpleLib.dll")]
  23.         public static extern int LongFirst(long num, string str);
  24.     }
  25. }
By the way, I wondered if this was a Unicode to ANSI marshalling problem, so I rebuilt the DLL as Unicode, and modified the two DllImport attributes to include CharSet=CharSet.Unicode. The same erroneous behavior occurs.

Any ideas?

thanks,
--John.
Nov 26 '08 #1
1 2727
I found the answer to my problem! The numeric parameter in the DLL is declared as a long, which is a 32-bit integer. That same numeric parameter in the DllImport declaration is also declared as a long. However, its size in C# is actually a 64-bit integer (twice as big). So when this was passed as the first parameter, it passed in a 64-bit value, followed by the string. When the DLL read in the argument list, it read the first 32 bits, followed by the remaining 32-bits (which were zero), mistakenly thinking it was the string. Ouch!

Here's how they should have been declared in the C# code:

// "num" is declared as a long (32-bit int) in the DLL, but this is
// declared as an int (32-bit int) here in C#:
[DllImport("SimpleLib.dll")]
public static extern int StrFirst(string str, int num);
[DllImport("SimpleLib.dll")]
public static extern int LongFirst(int num, string str);
Nov 26 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: arotem | last post by:
Hi, I am trying to call an unbound method (PrintInput) with the object instance as the first argument but getting the following error: "TypeError: unbound method PrintInput() must be called with...
6
by: C# newbie | last post by:
Hello group, When I run an XPATH query first as: //*[contains(translate(.,\"ABCDEFGHIJKLMNOPQRSTUVWXYZ\", \"abcdefghijklmnopqrstuvwxyz\")
1
by: Brian Anderson | last post by:
Hello, I have a native, C++ console app that uses ~26MB of RAM when it runs. It uses quite a lot of RAM to make some math but will never xceed 26MB. Now I've made a dll out of this code and...
1
by: =?Utf-8?B?b2xjQG9ubGluZS5ub3NwYW0=?= | last post by:
I was using managed code DLL from C++ to VB projects. But it seems the configuration of the project is wrong that I can not import the DLL with DLLImport.
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.