C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 4th, 2003, 06:39 PM
phelgren phelgren is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 3 phelgren User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Angry Access Violation using strcpy

I am struggling with my meager C++ skills to solve a mystery but after 3 days at this, I am stumped. I built a C++ DLL to wrap an existing VB dll (that had it's own challenges) but I don't THINK that is the problem. The C++ DLL simply calls one function from the VB DLL which returns a value. I want to return THAT value (a Variant) to my program. After building and compiling it, it tested it by calling it from a quick and dirty VB app and it worked fine. However, when I call it from actual app that uses it, I get a access violation (0xcccccccc) but only in certain circumstances. Here is the code:

extern "C" BSTR PASCAL EXPORT BldSubs(VARIANT lAbsID,VARIANT lLine,VARIANT bHTML)

{

int msgid = 0;
char szResult[2] = "G";
VARIANT r0;
_IOServices p;
char szGood[2] = "G";
char szBad[2] = "B";

p.CreateDispatch("ASAAPIO.IOServices");

r0 = p.bldsublist(&lAbsID,&lLine,&bHTML);

msgid = MessageBox(NULL,szResult,"Test DLL 1",MB_OKCANCEL);

strcpy(szResult,r0.pcVal);

msgid = MessageBox(NULL,szResult,"Test DLL 2",MB_OKCANCEL);

return ::SysAllocString((OLECHAR*)szResult);

}

it is the strcpy function that is causing it to croak. If I change the strcpy statement to strcpy(szResult,szBad); then the code will properly run and I will get both a message box with "G" and then "B". However, the strcpy of the variant seems to blow things up.

The program doing the calling is a Basicscript app which has successfully called functions from another C++ DLL. I'll get an access violation is I use the strcmp, strcpy or strcat. Anything that attempts to access the returned variant give me an access violation.

So, what have I been missing here? Something isn't happening when I run this in basicscript, something is not initialized, but what is it? Again, this DLL runs fine when called from a VB app.

Thanks,

Pete

Reply With Quote
  #2  
Old June 4th, 2003, 06:47 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 6th Plane (7500 - 7999 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,587 Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 21 h 21 m 5 sec
Reputation Power: 997
What if you use strncpy instead of strcpy when copying r0.pcVal, something like this:
strncpy(szResult, ro.pcVal, 1);
szResult[1]='\0'; // To be really safe!

My guess is that the strcpy is copying more characters than szResult can hold.

Reply With Quote
  #3  
Old June 4th, 2003, 06:56 PM
phelgren phelgren is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 3 phelgren User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I thought about that, but didn't implement it thinking that if the buffer was being overrun, it would die in a call from VB just like it would in the basicscript call.

But I tried it anyway and I still get the error.

I suspect (and I am not the least bit knowledgeable on this) that it a stack or alignment problem. Maybe Basicscript can't access the stack in certain regions or something like that...

Thanks

Reply With Quote
  #4  
Old June 5th, 2003, 12:48 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,861 dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 22 h 23 m 55 sec
Reputation Power: 462
Just a stupid thought:
In C/C++, a string is stored as an array of characters with the string in elements 0 through (len-1) and with a NUL ('\0') in array element len. The length is given implicitly by the placement of that NUL.

How does VB handle strings? In the other forms of BASIC I've worked with, a string was stored in an array of characters in elements 1 through len and with the string length stored explicitly in element 0. There is no NUL in that character array. And that the string length is stored in a byte is the reason why string lengths were limited to 255.

So, does VB handle strings in the traditional way or in the C/C++ way? Therein may lie the incompatibility that allowed the DLL to work when called in VB yet crash in C++.

Reply With Quote
  #5  
Old June 5th, 2003, 11:04 AM
phelgren phelgren is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 3 phelgren User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Actually, that is a pretty good thought and I have spent some time trying to understand the Variant data type and specifically how they relate to strings. I was wondering if the return value type was somehow part of the problem here, so I changed the return type to be a BOOL in my C++ DLL and changed both my Test VB program and then the BasicScript app to look for a boolean return type. The VB app works, the BasicScript app gives me the same Access Violation error. I think the 0xcccccccc address is telling me that the variable isn't initialized perhaps it has gone out of scope although I pass the values byVal, perhaps the return value is coming back byRef and the called DLL has already released the memory...but my local variable should be holding the value...unless the value returned IS the memory address and is no longer valid...hmmmm.

Like I said, C++ is not my strong suit..I'll do a little more research on the "return value" side of this equation. Its obvious that VB and BasicScript handle these values differently, I just wonder what that difference is...or perhaps it has nothing to do with the return value and is just a problem with my DLL (MFC vs Win32?).

Thanks.. and keep those ideas coming.

Pete

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Access Violation using strcpy


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT