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
- SIMPLELIB_API int StrFirst(LPTSTR str, long num);
Expand|Select|Wrap|Line Numbers
- SIMPLELIB_API int LongFirst(long num, LPTSTR str);
I also have a Win32 Console Application that calls these functions in the DLL, like this:
Expand|Select|Wrap|Line Numbers
- int _tmain(int argc, _TCHAR* argv[])
- {
- LPTSTR MyStr = _T("Calling from TestApp");
- long MyNum = 1234;
- int RetVal = 1;
- RetVal = StrFirst(MyStr, MyNum);
- RetVal = LongFirst(MyNum, MyStr);
- return 0;
- }
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
- using System.Runtime.InteropServices; // DllImport
- namespace TestApp.Net
- {
- class Program
- {
- static void Main(string[] args)
- {
- string str = "Calling from TestApp.Net";
- long num = 1234;
- int RetVal = 1;
- // The following call succeeds:
- RetVal = StrFirst(str, num);
- // But this fails. The 2nd arg appears in the DLL as a NULL. Why?
- RetVal = LongFirst(num, str);
- }
- [DllImport("SimpleLib.dll")]
- public static extern int StrFirst(string str, long num);
- [DllImport("SimpleLib.dll")]
- public static extern int LongFirst(long num, string str);
- }
- }
Any ideas?
thanks,
--John.