Tech
Forums
Jobs
Books
Events
Videos
Conference
Annual Conference
Bcrypt
Ai Conference
Live
More
Interviews
Certification
Training
Career
Members
News
Blogs
Contribute
An Article
A Blog
A Video
An Ebook
An Interview Question
Register
Login
1
Answer
Problem in Getting the exact char[] data from C++ to C# String
Ask a question
Ask a question
Upload Source Code:
Select only zip and rar file.
sunil bansal
17y
2.8k
1
Reply
Hi,
I am facing a problem while accessing a C++ dll from C# code.
Following is my C++ structure I am using...
typedef struct
{
LONG lPaBitNr;
UCHAR szPaAbr[8];
} AudioUnit_T_DataType;
and corresponding structure I am using in Dot Net is:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct AudioUnit_T_DataType
{
public int paBitNr;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=8)]
public String paAbr;
}
and following is my declaration of the function of C++ dll.
[DllImport("MyLib.dll", EntryPoint = "MyFunc")]
public static extern int MyFunc(
[MarshalAs(UnmanagedType.AsAny)]
object key,
[MarshalAs(UnmanagedType.AsAny),In, Out]
object data,
int size
);
size is the respective size of the structure (no of bytes to copy in data object)
When I call the MyFunc function with object data as AudioUnit_T_DataType structure,, i got all the data but I always loose the last character (8th) in the String paAbr.
i.e. suppose, the value of szPaAbr[8] was "abcdefgh"
then, after the call of MyFunc function, i got "abcdefg" value.. 'h' is lost (might be it is replaced by null character...)
What should I do to overcome this issue.
I dont want to change the lenght of array from 8 to 9.
Any help is appreciable.
Post
Reset
Cancel
Answers (
1
)
Next Recommended Forum
Retrieve data from SQL stored procedure in Excel spreadsheet
How can I insert Controls (eg. listbox, combobox) in Excel automation with events using C#