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

Specified Cast is not Valid (Managed to Unmanaged)

I have a c# class library that I call to centralize some code amongst projects. It returns an arraylist back to the calling function that contains class data. Anyway when I call it I can receive it as an object and the data looks fine. However when I do the type casting I get the above error. Any ideas. Below is the code. At the bottom you can see the comment on the cast that blows. I have even tried it without using the Object*. I just added that to make sure the get_item call was working properly.

Bob

C# side.

[ StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto) ]
public class LOGFONT
{
public int lfHeight = 0;
public int lfWidth = 0;
public int lfEscapement = 0;
public int lfOrientation = 0;
public int lfWeight = 0;
public byte lfItalic = 0;
public byte lfUnderline = 0;
public byte lfStrikeOut = 0;
public byte lfCharSet = 0;
public byte lfOutPrecision = 0;
public byte lfClipPrecision = 0;
public byte lfQuality = 0;
public byte lfPitchAndFamily = 0;
[ MarshalAs(UnmanagedType.ByValTStr, SizeConst=32) ] public string lfFaceName = null;
}

public class CStyleSheet
{
public string strTagname = "";
public string strTagContext = "";
public LOGFONT lfFont = new LOGFONT(); // Structure defining the font attributes for each tag us to be logical
public System.Drawing.Color FontColor, BackgroundColor, rgbAlternateColor;
public short LineSpace = 0;
public short SpaceAbove = 0;
public short SpaceBelow = 0;
public short IndentLeft = 0;
public short IndentRight = 0;
public short IndentFirst = 0;
public short[] tabsArray = new short[10];
public short justification = 0;
public ushort Flags = 0;
public ushort ActualFlags = 0;
public string strTextBefore = "";
public string strTextAfter = "";
public string Emph = "";
public ushort InheritanceFlags = 0; // Use to be WORD
public string LinkAttribute = "";
public byte BorderStyle = 0;
public byte SpecialEmph = 0;
public byte ChangeLevel = 0;
public ushort AutonumberStyle = 0;
public int iMatchWeight = 0;

public CStyleSheet()
{
}
}
Visual C++ side.
__gc class CStyleSheet
{
public:
System::String *strtagname;
System::String *strTagContext;
LOGFONT lfFont; // Structure defining the font attributes for each tag
System::Drawing::Color FontColor, BackgroundColor, rgbAlternateColor;
short LineSpace;
short SpaceAbove;
short SpaceBelow;
short IndentLeft;
short IndentRight;
short IndentFirst;
short tabsArray __nogc[MAXTABS];
short justification;
USHORT Flags;
USHORT ActualFlags;
System::String *strTextBefore;
System::String *strTextAfter;
System::String *Emph;
USHORT InheritanceFlags; // Use to be WORD
System::String *LinkAttribute;
byte BorderStyle;
byte SpecialEmph;
byte ChangeLevel;
USHORT AutonumberStyle;
int iMatchWeight;

};
Header defs:

gcroot<System::Collections::ArrayList*> StyleArray;
gcroot<CStyleSheet*> pGCStyleStruct;
Cpp calls

Load Works just fine.

StylesIO::CStylesIO *myStyles = new StylesIO::CStylesIO();
StyleArray = myStyles->LoadStyleSheet(strPath, theApp.m_pActiveWnd);
Type cast where it blows
Object* myObject;
myObject = pDoc->StyleArray->get_Item(1);
CStyleSheet *sty1 = new CStyleSheet ();
sty1 = (CStyleSheet *) myObject; ********* Blows here.

Nov 17 '05 #1
0 1534

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

Similar topics

0
by: Tao | last post by:
I just upgraded .NET framework to 1.1 and VS.Net to 2003 version and tried to test it out. I created an ASP.NET project using the wizard and tried to run it by hitting "F5". I got an exception:...
6
by: Raj | last post by:
I have a c# program in which I used cast. That worked perfectly on .Net platform 1.0. When I moved to .Net 2003, I am getting the execption "Specified cast is not valid". I have not made any...
4
by: Tyro | last post by:
Can someone shed some light on my error here? Thanks! Specified cast is not valid. Exception Details: System.InvalidCastException: Specified cast is not valid. Source Error: Stack Trace:
3
by: PK9 | last post by:
I am looking for assistance in pinpointing the cause of the following exception. I am getting a "Specified Cast is not valid" exception on my page. I am trying to populate a datagrid. One of my...
2
by: Fabian | last post by:
Hi, I work with asp.net 2.0 and I have a intermittent error, only happens a few times a day. In the page I evaluate a Query String and then I get data form a database. The code snipped: ...
3
by: VB Programmer | last post by:
I am setting up forms authentication. In my code I keep getting this error. Any ideas? Error.... Server Error in '/LandOLots' Application....
0
by: QA | last post by:
I am using a Business Scorecard Accelarator in a Sharepoint Portal 2003 using SQL Server 2005 I am getting the following error: Error,5/7/2005 10:50:14 AM,580,AUE1\Administrator,"Specified cast is...
0
by: Alan Z. Scharf | last post by:
this question in datagrid group for several days with no repsonse. I'm hoping for an answer her because of greater activity in this group. No cross-posting intended. Thanks....
3
by: =?Utf-8?B?UGF1bCBQcmV3ZXR0?= | last post by:
I'm attempting to use LINQ to insert a record into a child table and I'm receiving a "Specified cast is not valid" error that has something to do w/ the keys involved. The stack trace is: ...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
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,...
0
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...

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.