|
Now I'm puzzled again. Are you saying some elementary bit manipulation and file I/O is much faster in 3.5 than it is in 1.1? I'm not complaining, it just is news to me (not very relevant though, as I retired all 1.x stuff years ago, needing some of the 2.0 classes).
|
|
|
|
|
I've got char[] buffer = new char[N]; with some unicode text.
The message is less than N and all other elements are 0 in the buffer .
But when I create string from it with string str = new string(buffer); it appends all N elements including terminating zero characters that string looks like: "message\0\0\0\0\0\0\0\0\0\0\0 ... more \0 chars .
Why it does not terminate it?
Чесноков
|
|
|
|
|
This should do the job:
string str = new string(buffer).TrimEnd('\0');
Or, if you know the desired length of the string, you can try this:
string str = new string(buffer, 0, length);
|
|
|
|
|
Because C# strings are not null terminated, unlike C / C++. Because the char[] has a length, each character is transferred into a new string of the same length.
Use Trim or the buffer convert as Kubajzz suggested.
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
|
|
|
|
|
Hi, everyone!
Now I am developing an application using TAPI. I want to import some functions from cellcore.dll. But I don't know how to write the prototype of these functions. The functions I want to import are: lineInitializeEx, lineNegotiateAPIVersion, lineOpen, lineSetEquipmentState, lineGetEquipmentState, lineRegister, lineClose and lineShutdown. For example, I try to import lineInitializeEx with the code below:
[DllImport("cellcore.dll")]
private static extern long lineInitializeEx(ref uint phLineApp,IntPtr hInstance,IntPtr CallBackFunc,string strAppName,
ref uint dwDevNum,ref uint pdwApiVersion,ref LINEINITIALIZEEXPARAMS lineParams);
I had declare LINEINITIALIZEEXPARAMS struct previous. It can compiled. But can't be executed. The error message say it can't find the entry point for this function. I wonder the prototype is wrong. How to write it? Thx!
There is some white cloud floating on the blue sky. That's the landscape I like.
|
|
|
|
|
You may have some errors in what you've shown and what you have elsewhere.
For example, CallBackFunc generally would be a delegate on the managed side and you are returning long - LONG in C/C++ is an int/uint in C#.
I've never used the library, but from looking at MSDN, this is how I'd attempt it and tweak where needed. Always use SetLastError so you can get the last error from the system when thing go bang!
private delegate void lineCallbackFunc(
uint hDevice,
uint dwMsg,
uint dwCallbackInstance,
uint dwParam1,
uint dwParam2,
uint dwParam3);
[StructLayout(LayoutKind.Sequential)]
private struct LINEINITIALIZEEXPARAMS
{
public uint dwTotalSize;
public uint dwNeededSize;
public uint dwUsedSize;
public uint dwOptions;
public IntPtr hEvent;
public IntPtr hCompletionPort;
public uint dwCompletionKey;
}
[DllImport("cellcore.dll", SetLastError = true)]
private static extern uint lineInitializeEx(
out IntPtr lphLineApp,
IntPtr hInstance,
lineCallbackFunc lpfnCallback,
StringBuilder lpszFriendlyAppName,
out uint lpdwNumDevs,
out uint lpdwAPIVersion,
ref LINEINITIALIZEEXPARAMS lpLineInitializeExParams
);
|
|
|
|
|
I am pleasure to see your answer. Thank you!
But I have still a problem. As the code you show, the struct LINEINITIALIZEEXPARAMS has two elements: hEvent and hCompletionPort. But in C++, this struct has a union. The element in it have the same name as your code. I know the keyword "union" isn't exist in C#. But I think this struct shouldn't be declare as this.
Additional, I can't still execute this program. The error is still "No entry point found for lineInitializeEx". I hope you can help me again. Thx!
There is some white cloud floating on the blue sky. That's the landscape I like.
|
|
|
|
|
You are correct about the struct - I missed the union
[StructLayout(LayoutKind.Explict)]
private struct LINEINITIALIZEEXPARAMS
{
[FieldOffset(0)]
public uint dwTotalSize;
[FieldOffset(4)]
public uint dwNeededSize;
[FieldOffset(8)]
public uint dwUsedSize;
[FieldOffset(12)]
public IntPtr hEvent;
public IntPtr hCompletionPort;
[FieldOffset(12)]
public uint dwCompletionKey;
}
Are you sure you have the dll installed as it seems windows can't find it? If it's on your system, try using an absolute path.
|
|
|
|
|
Excellent thread, except:
DaveyM69 wrote: [FieldOffset(12)]
public uint dwCompletionKey;
IMO it is either 16 (Win32) or 20 (Win64).
|
|
|
|
|
I'm never 100% sure about unions not being a C guy. I believe that at byte 12 will be either hEvent OR dwCompletionKey .
The way I understand it is that if the handles aren't used then the size of the struct will be 16 bytes, if the handles aren't are used, then the size will be 20 bytes if Win32 or 28 if Win64. The location of dwCompletionKey /hEvent will always be 12 and only the size of the struct will be different.
Please correct me if I'm wrong!
|
|
|
|
|
Hi Dave,
This is the original definition from MSDN:
typedef struct lineinitializeexparams_tag {
DWORD dwTotalSize;
DWORD dwNeededSize;
DWORD dwUsedSize;
DWORD dwOptions;
union {
HANDLE hEvent;
HANDLE hCompletionPort;
} Handles;
DWORD dwCompletionKey;
} LINEINITIALIZEEXPARAMS, *LPLINEINITIALIZEEXPARAMS;
in C/C++ every struct member that is not inside a union block will be layed out sequentially; and everything inside a union block will share the same memory (and needs the size of the largest item), so here hEvent and hCompletionPort are overlapping, however dwCompletionKey (not being part of the union) must be at the next suitable address following the union, hence at start of union item plus 4 ot 8 depending on pointer sizes.
BTW: there seems to be a dwOptions member too, which hasn't been mentioned before!
|
|
|
|
|
Hmm... intersting. The only time I've written code requiring it was for MMTIME[^] which I have like this
[StructLayout(LayoutKind.Explicit)]
internal struct MMTIME
{
[FieldOffset(0)]
public uint wType;
[FieldOffset(4)]
public uint ms;
[FieldOffset(4)]
public uint sample;
[FieldOffset(4)]
public uint cb;
[FieldOffset(4)]
public uint ticks;
[FieldOffset(4)]
public byte hour;
[FieldOffset(5)]
public byte min;
[FieldOffset(6)]
public byte sec;
[FieldOffset(7)]
public byte frame;
[FieldOffset(8)]
public byte fps;
[FieldOffset(9)]
private byte dummy;
[FieldOffset(10)]
private ushort pad;
[FieldOffset(4)]
public uint songptrpos;
}
Luc Pattyn wrote: there seems to be a dwOptions member too, which hasn't been mentioned before!
I had that in the original reply[^] but missed it when I looked at the union
|
|
|
|
|
That looks OK; there is one union, all its members are starting at the same offset (4), and nothing is following the union (both inner structs are inside the union, the union is the last member of the outer struct).
|
|
|
|
|
|
According to P/Invoke.net, lineInitializeEx is in coredll.dll NOT cellcore.dll
|
|
|
|
|
Yes, you are right. I had success to execute it now. Thank you very much!
There is some white cloud floating on the blue sky. That's the landscape I like.
|
|
|
|
|
Following the discussion I had with Luc, I've done some investigation into the way uinions work, and he is correct, the parameters inside the union block start at the same place in memory - in otherwords, the struct will contain one of the handles but not both so the best way to declare it is:
[StructLayout(LayoutKind.Sequential)]
private struct LINEINITIALIZEEXPARAMS
{
public uint dwTotalSize;
public uint dwNeededSize;
public uint dwUsedSize;
public uint dwOptions;
public IntPtr handle;
public uint dwCompletionKey;
}
|
|
|
|
|
I'm glad to see your answer. But now I meet a new problem, that I can't get HINSTANCE. I had find some ways in MSDN. But no available way I found. Could you be kind to help me again? Thank you!
There is some white cloud floating on the blue sky. That's the landscape I like.
modified on Monday, September 13, 2010 2:03 AM
|
|
|
|
|
MSDN says: "Instance handle of the client application or DLL. The application or DLL can pass NULL for this parameter, in which case TAPI uses the module handle of the root executable of the process (for purposes of identifying call handoff targets and media mode priorities).
The clue is "can pass NULL", which in the case of PInvoke handles is IntPtr.Zero . That should work.
If not, have a look at the Process.Handle[^] property - perhaps this will work?
IntPtr hInstance = System.Diagnostics.Process.GetCurrentProcess().Handle;
and pass hInstance as the parameter..., I prefer IntPtr.Zero though!
|
|
|
|
|
I had tested the code above. I feel sorry that it can't work. Because I am developing application for Windows Mobile. The property System.Diagnostics.Process.GetCurrentProcess().Handle can't be get in Mobile. I had try to give hInstance with the value of new IntPtr(GetModuleHandle(new IntPtr(0)).ToInt32()) . But when I call lineInitializeEx with it, the return value is an error code. That tell me the value of handle is invalid(0x80000035). Base of all, I think I can't call lineInitializeEx with C# at all. Isn't it?
There is some white cloud floating on the blue sky. That's the landscape I like.
|
|
|
|
|
Have you tried with IntPtr.Zero or new IntPtr(0)
What is the error code? It should be one of these:
public const uint LINEERR_NOERROR = 0x00000000;
public const uint LINEERR_INVALAPPNAME = 0x80000015;
public const uint LINEERR_INIFILECORRUPT = 0x8000000E;
public const uint LINEERR_INVALPARAM = 0x80000032;
public const uint LINEERR_INVALPOINTER = 0x80000035;
public const uint LINEERR_NOMEM = 0x80000044;
public const uint LINEERR_OPERATIONFAILED = 0x80000048;
public const uint LINEERR_REINIT = 0x80000052;
If the version of WinMob you're targetting supports TAPI then it will work once you get it just right!
Maybe post your lineInitializeEx and LINEINITIALIZEEXPARAMS ...
|
|
|
|
|
I had try to use the value of IntPtr.Zero and new IntPtr(0) before. The return value is error too. In all cases, the error code returned is still public const uint LINEERR_INVALPOINTER = 0x80000035; . I am sure my Window Mobile support TAPI. I had developed a TAPI application with C++. Thank you!
There is some white cloud floating on the blue sky. That's the landscape I like.
|
|
|
|
|
Can you post your function and struct declarations?
|
|
|
|
|
public bool OpenLine()
{
UInt32 dwDevNum = 0;
UInt32 dwApiVerSion = TAPI_CURRENT_VERSION;
LINEINITIALIZEEXPARAMS dtParams = new LINEINITIALIZEEXPARAMS();
dtParams.dwTotalSize = (UInt32)Marshal.SizeOf(dtParams);
dtParams.dwOptions = 1;
IntPtr handles = new IntPtr(0);
IntPtr hInstance = IntPtr.Zero;
unsafe
{
fixed(IntPtr *hLineApp = &m_hLineApp)
{
long lResult = lineInitializeEx(hLineApp, hInstance, new IntPtr(0), new IntPtr(0),
new IntPtr(&dwDevNum), new IntPtr(&dwApiVerSion), new IntPtr(&dtParams));
if (lResult == 0)
{
return true;
}
}
return false;
}
}
[StructLayout(LayoutKind.Sequential)]
private struct LINEINITIALIZEEXPARAMS
{
public uint dwTotalSize;
public uint dwNeededSize;
public uint dwUsedSize;
public uint dwOptions;
public IntPtr hMultiUse;
public uint dwCompletionKey;
}
[DllImport("coredll.dll", SetLastError = true)]
unsafe private static extern uint lineInitializeEx(
IntPtr *lphLineApp,
IntPtr hInstance,
IntPtr pfnCallback,
IntPtr FriendAppName,
IntPtr pNumDevs,
IntPtr pAPIVersion,
IntPtr pLineInitializeExParams);
There is some white cloud floating on the blue sky. That's the landscape I like.
|
|
|
|
|
Not too sure where the problem is. The first thing I'd do is get rid of the *, &, unsafe and fixed pointer stuff.
It's rarely needed as the built in marshaller can handle this stuff way better. I doubt it's the cause of your problems but declaring dwDevNum and creating a pointer from it's address is not going to work.
There are two keywords for passing value types by reference (thier pointer), ref and out .
If a value needs to be passed into a function then use ref . If the function doesn't need a value but there will be one there after the function returns then use out . In the case of dwDevNum this should be out but lpdwAPIVersion and dtParams should be ref . As IntPtr is a value type the pointer to a pointer for lphLineApp can be done using ref /out as well.
To the solution... have a look at this[^]. The guy says his C# code works.
|
|
|
|
|