473,791 Members | 3,113 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

PInvoke TCHAR[] ??? Having trouble with this.

Hi all,
I'm having trouble PInvoking a TCHAR[ ] within a struct.
I'll paste the specific struct's API definition below.
I've tried so many numerous variations.
The main Win32 error I get is 0x3f0 / 515L which amounts to ERROR_NO_TOKEN.
Every single instance of this in the past was due to mistakes I made while
within PInvoked structs.
Is anybody able to point me to documentation or just tell me outright how to
do this?
I've been PInvoking a bit for the last few months but never ran accross this
specific data type--not until now.
Any questions or request for more info? Feel free to ask away.

Thanks a lot,
-MH
typedef struct _SP_DRVINFO_DAT A {
DWORD cbSize;
DWORD DriverType;
ULONG_PTR Reserved;
TCHAR Description[LINE_LEN];
TCHAR MfgName[LINE_LEN];
TCHAR ProviderName[LINE_LEN];
FILETIME DriverDate;
DWORDLONG DriverVersion;
} SP_DRVINFOR_DAT A, *PSP_DRVINFO_DA TA;
Sep 20 '06 #1
3 5744

"Michael" <mh***@domain.c omwrote in message
news:u3******** ******@TK2MSFTN GP05.phx.gbl...
| Hi all,
|
|
| I'm having trouble PInvoking a TCHAR[ ] within a struct.
| I'll paste the specific struct's API definition below.
| I've tried so many numerous variations.
| The main Win32 error I get is 0x3f0 / 515L which amounts to
ERROR_NO_TOKEN.
| Every single instance of this in the past was due to mistakes I made while
| within PInvoked structs.
| Is anybody able to point me to documentation or just tell me outright how
to
| do this?
| I've been PInvoking a bit for the last few months but never ran accross
this
| specific data type--not until now.
| Any questions or request for more info? Feel free to ask away.
|
| Thanks a lot,
| -MH
|
|
| typedef struct _SP_DRVINFO_DAT A {
| DWORD cbSize;
| DWORD DriverType;
| ULONG_PTR Reserved;
| TCHAR Description[LINE_LEN];
| TCHAR MfgName[LINE_LEN];
| TCHAR ProviderName[LINE_LEN];
| FILETIME DriverDate;
| DWORDLONG DriverVersion;
| } SP_DRVINFOR_DAT A, *PSP_DRVINFO_DA TA;
|
|

Please show us your C# struct declaration, and the API's declaration (the
DllImport statement).

A win32 error code is a single integer value, but you say ... Win32 error I
get is 0x3f0 / 515L ..., mind to explain what you really mean here?

Willy.


Sep 20 '06 #2

"Willy Denoyette [MVP]" <wi************ *@telenet.bewro te in message
news:uD******** *****@TK2MSFTNG P05.phx.gbl...
>
"Michael" <mh***@domain.c omwrote in message
news:u3******** ******@TK2MSFTN GP05.phx.gbl...
| Hi all,
|
|
| I'm having trouble PInvoking a TCHAR[ ] within a struct.
| I'll paste the specific struct's API definition below.
| I've tried so many numerous variations.
| The main Win32 error I get is 0x3f0 / 515L which amounts to
ERROR_NO_TOKEN.
| Every single instance of this in the past was due to mistakes I made
while
| within PInvoked structs.
| Is anybody able to point me to documentation or just tell me outright
how
to
| do this?
| I've been PInvoking a bit for the last few months but never ran accross
this
| specific data type--not until now.
| Any questions or request for more info? Feel free to ask away.
|
| Thanks a lot,
| -MH
|
|
| typedef struct _SP_DRVINFO_DAT A {
| DWORD cbSize;
| DWORD DriverType;
| ULONG_PTR Reserved;
| TCHAR Description[LINE_LEN];
| TCHAR MfgName[LINE_LEN];
| TCHAR ProviderName[LINE_LEN];
| FILETIME DriverDate;
| DWORDLONG DriverVersion;
| } SP_DRVINFOR_DAT A, *PSP_DRVINFO_DA TA;
|
|

Please show us your C# struct declaration, and the API's declaration (the
DllImport statement).

A win32 error code is a single integer value, but you say ... Win32 error
I
get is 0x3f0 / 515L ..., mind to explain what you really mean here?

Willy.

Willy,

Sorry for the confusion. 0x3f0 = 515. I put both depending on what's easier
to recognize.
In the the winerror.h file, it's #defined with 515L. In VS2005 I see it
repored in hex value--
just trying to cover all bases.

the API's declaration is above. My C# declaration is below along w/ the
DllImport of the
function consuming it. My SP_DEVINFO_DATA is used in other Win32 PInvoke
function calls and works so far :).

Thanks,
[DllImport("setu papi.dll", CharSet = CharSet.Ansi, SetLastError = true)]
internal static extern System.Boolean SetupDiGetSelec tedDriver
(
System.IntPtr DeviceInfoSet,
ref SP_DEVINFO_DATA DeviceInfoData,
out SP_DRVINFO_DATA DriverInfoData
);
[StructLayout(La youtKind.Sequen tial)]
internal class SP_DRVINFO_DATA
{
public System.UInt32 cbSize;
public System.UInt32 DriverType;
public System.IntPtr Reserved;
[MarshalAs(Unman agedType.ByValT Str, SizeConst = 256)]
public System.String Description;

[MarshalAs(Unman agedType.ByValT Str, SizeConst = 256)]
public System.String MfgName;

[MarshalAs(Unman agedType.ByValT Str, SizeConst = 256)]
public System.String ProviderName;

public FileTime DriverDate;

public System.UInt64 DriverVersion;
}

[StructLayout(La youtKind.Sequen tial)]
internal struct SP_DEVINFO_DATA
{
public System.UInt32 cbSize;
public System.Guid ClassGuid;
public System.UInt32 DevInst;
public System.IntPtr Reserved;
}



Sep 20 '06 #3

"Michael" <mh***@domain.c omwrote in message
news:Om******** ******@TK2MSFTN GP06.phx.gbl...
|
| "Willy Denoyette [MVP]" <wi************ *@telenet.bewro te in message
| news:uD******** *****@TK2MSFTNG P05.phx.gbl...
| >
| "Michael" <mh***@domain.c omwrote in message
| news:u3******** ******@TK2MSFTN GP05.phx.gbl...
| | Hi all,
| |
| |
| | I'm having trouble PInvoking a TCHAR[ ] within a struct.
| | I'll paste the specific struct's API definition below.
| | I've tried so many numerous variations.
| | The main Win32 error I get is 0x3f0 / 515L which amounts to
| ERROR_NO_TOKEN.
| | Every single instance of this in the past was due to mistakes I made
| while
| | within PInvoked structs.
| | Is anybody able to point me to documentation or just tell me outright
| how
| to
| | do this?
| | I've been PInvoking a bit for the last few months but never ran
accross
| this
| | specific data type--not until now.
| | Any questions or request for more info? Feel free to ask away.
| |
| | Thanks a lot,
| | -MH
| |
| |
| | typedef struct _SP_DRVINFO_DAT A {
| | DWORD cbSize;
| | DWORD DriverType;
| | ULONG_PTR Reserved;
| | TCHAR Description[LINE_LEN];
| | TCHAR MfgName[LINE_LEN];
| | TCHAR ProviderName[LINE_LEN];
| | FILETIME DriverDate;
| | DWORDLONG DriverVersion;
| | } SP_DRVINFOR_DAT A, *PSP_DRVINFO_DA TA;
| |
| |
| >
| Please show us your C# struct declaration, and the API's declaration
(the
| DllImport statement).
| >
| A win32 error code is a single integer value, but you say ... Win32
error
| I
| get is 0x3f0 / 515L ..., mind to explain what you really mean here?
| >
| Willy.
| >
| >
| Willy,
|
| Sorry for the confusion. 0x3f0 = 515. I put both depending on what's
easier
| to recognize.
| In the the winerror.h file, it's #defined with 515L. In VS2005 I see it
| repored in hex value--
| just trying to cover all bases.
|
| the API's declaration is above. My C# declaration is below along w/ the
| DllImport of the
| function consuming it. My SP_DEVINFO_DATA is used in other Win32 PInvoke
| function calls and works so far :).
|
| Thanks,
|
|
| [DllImport("setu papi.dll", CharSet = CharSet.Ansi, SetLastError = true)]
| internal static extern System.Boolean SetupDiGetSelec tedDriver
| (
| System.IntPtr DeviceInfoSet,
| ref SP_DEVINFO_DATA DeviceInfoData,
| out SP_DRVINFO_DATA DriverInfoData
| );
|
|
| [StructLayout(La youtKind.Sequen tial)]
| internal class SP_DRVINFO_DATA
| {
| public System.UInt32 cbSize;
| public System.UInt32 DriverType;
| public System.IntPtr Reserved;
|
|
| [MarshalAs(Unman agedType.ByValT Str, SizeConst = 256)]
| public System.String Description;
|
| [MarshalAs(Unman agedType.ByValT Str, SizeConst = 256)]
| public System.String MfgName;
|
| [MarshalAs(Unman agedType.ByValT Str, SizeConst = 256)]
| public System.String ProviderName;
|
| public FileTime DriverDate;
|
| public System.UInt64 DriverVersion;
| }
|
| [StructLayout(La youtKind.Sequen tial)]
| internal struct SP_DEVINFO_DATA
| {
| public System.UInt32 cbSize;
| public System.Guid ClassGuid;
| public System.UInt32 DevInst;
| public System.IntPtr Reserved;
| }
Hmmm... 0x3fO = 1008 ;-)
Are you sure you pass a valid DeviceInfoSet handle? The error code 1008
cannot be due to an error in SP_DRVINFO_DATA , it's a return argument.

To prove yourself:
Try calling the API passing a IntPtr as third argument (change the API
declaration accordingly)..

Allocate a buffer large enough to hole the structure and pass the IntPtr to
the API.
....
IntPtr DriverInfoData = Marshal.AllocHG lobal(2048);
... SetupDiGetSelec tedDriver(...,. ., out DriverInfoData );
when done, check the return and when OK, marshal the buffer to the structure
using Marshal.PtrToSt ructure.
and free the allocated unmanaged buffer by calling
Marshal.FreeHGl obal(DriverInfo Data);
Some remarks:
- Use the UNICODE version of the API's whenever available and do it
consequetally throughout your code, so here use CharSet.Unicode for both the
API and the structure declaration.

- Make SP_DRVINFO_DATA a structure like :

[StructLayout(La youtKind.Sequen tial, CharSet=CharSet .Unicode)]
internal struct SP_DRVINFO_DATA

Willy.
Sep 21 '06 #4

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

Similar topics

7
20941
by: ree | last post by:
I need to use a function from a wininet library, it requires the use of const TCHAR. The problem is the value needs to be calculated, so at the end I have this string but I am having troubles converting it into a const TCHAR so I can use it in the function. TIA
4
10186
by: sorty | last post by:
Hi, I have read in many places that TCHAR can be 'char' or 'wchar_t' depending on ANSI or UNICODE. I have also read that LPTSTR is a long pointer to a TCHAR. I am confused about the following. Please answer with yes/no and then elaborate as you please..
4
4624
by: muroogan | last post by:
I read a text file into a char variable char tchar; I want to trim leading spaces (Left trim).How can I do that in C++. Any input will be appreciated.
2
1789
by: SteveK | last post by:
I cannot for the life of me figure out how to TCHAR to be defined as an unsigned char as it is in out 6.0 project. I created a fresh VS 2003 project and played around with the settings and still cannot get TCHAR to be defined as anything other than wchar_t Here are the compiler options in my setting's command window: /Od /D "WIN32" /D "_WINDOWS" /D "_DEBUG" /D "_MBCS" /D "_AFXDLL" /Gm /EHsc
10
5831
by: gshy2014 | last post by:
Hi, all. IN Windows, TCHAR is translated to be wchar or char according to whether UNICODE is defined. Dose the complier handle the UNICODE(define or not define) according to the OS ? Many thanks
6
2211
by: Pucca | last post by:
I have a program that originally compiles into a exe file. I changed the compile option to generate dll file. This program calls a com component. Can I use pinvoke in C# to call it? The following is the main dll file. #include "stdafx.h" #define MAX_ADSPATH_CHARS 2048 //*************************************************************************** // local function prototypes
3
1707
by: Michael | last post by:
Hi all, I believe I'm not PInvoking this struct correctly. Here's the API definition. typedef struct _SP_DRVINFO_DATA { DWORD cbSize; DWORD DriverType; ULONG_PTR Reserved; TCHAR Description;
14
3797
by: Mohamed Mansour | last post by:
Hey there, this will be somewhat a long post, but any response is appreciated! I have done many PInvoke in the past from C++ to C#, but I did PInvoke within C# not C++/CLI. Can someone explain more why C++/CLI would be better to PInvoke than doing the PInvoke in C#? Because, usually in C# as you already know we use DLLImport and extern
0
1076
by: Aldev | last post by:
Hi There, I am quite new to PInvoke and calling COM functions from C#. I have managed so far to call some unmanaged functions ib my C++ dll from my C# code but at the moment I am having trouble with one particular function. I have a sample of the function being correctly defined in some VB6 code below.
0
9669
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9517
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10207
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9997
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9030
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7537
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6776
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5435
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2916
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.