VSTO calling unmanged function

Status
Not open for further replies.

scot_paro

Member
Hi, I'm really struggling with this so any help would be appreciated.


Outlook 2007, VS2008 Pro, VSTO 3, XP Pro x64 (dev) and x32 (test).




I've a C++ dll with a bunch of functions to handle the registry, some custom authentication of the product, and some comms. I'd rather not have to rewrite them in VB.NET/C# (I'm primarily a C++ developer). There are extern "C" wrappers for the C++ calls.




The product was originally done in VBA, and everything works fine there :)




I've written an add-in and I've got the pinvoke stuff in place. I can call Win32 functions - for testing, I pop up a message box and get the system directory. All is well, works exactly as I'd expect.


However, when I call the functions in my unmanaged dll, the functions get entered, but their arguments are garbage. I'm passing in a StringBuilder to get a string of text back, but writing to the StringBuilder parameter in the unmanged function (wchar_t*) causes an access violation.


I've written a test project to make sure there's nothing funny with my machine setup. I've got my unmanaged dll and a VB.NET console app. The unmanaged functions return an int, accept a string, modify the string as a side effect, and it all works fine.


But, when I do exactly the same thing in the add-in, it fails :(




Is there something I'm missing? Is this a CAS issue? Does VSTO just not like unmanaged code? :confused:




Thanks.


-R.
 
K

Ken Slovak - [MVP - Outlook]

What if instead of passing a StringBuilder object you pass a string and

Marshal it as UnmanagedType.BStr, does that work?

"scot_paro" <scot_paro.3y5jco@invalid> wrote in message

news:scot_paro.3y5jco@invalid...

> Hi, I'm really struggling with this so any help would be appreciated.
> Outlook 2007, VS2008 Pro, VSTO 3, XP Pro x64 (dev) and x32 (test).

> I've a C++ dll with a bunch of functions to handle the registry, some
> custom authentication of the product, and some comms. I'd rather not
> have to rewrite them in VB.NET/C# (I'm primarily a C++ developer).
> There are extern "C" wrappers for the C++ calls.

> The product was originally done in VBA, and everything works fine there
> :)

> I've written an add-in and I've got the pinvoke stuff in place. I can
> call Win32 functions - for testing, I pop up a message box and get the
> system directory. All is well, works exactly as I'd expect.
> However, when I call the functions in my unmanaged dll, the functions
> get entered, but their arguments are garbage. I'm passing in a
> StringBuilder to get a string of text back, but writing to the
> StringBuilder parameter in the unmanged function (wchar_t*) causes an
> access violation.
> I've written a test project to make sure there's nothing funny with my
> machine setup. I've got my unmanaged dll and a VB.NET console app. The
> unmanaged functions return an int, accept a string, modify the string as
> a side effect, and it all works fine.
> But, when I do exactly the same thing in the add-in, it fails :(

> Is there something I'm missing? Is this a CAS issue? Does VSTO just
> not like unmanaged code? :confused:

> Thanks.
> -R.

> > scot_paro
> >
 

scot_paro

Member
Thanks Ken, but it makes no difference at all. I've changed my test program and my application, and the test program works fine (as it always has) but the VSTO application fails with an access violation, as before.




The really confusing thing is that the first argument in the unmanaged functions is the address of an object obtained via the dll - I'm wrapping a C++ object - and this works fine.


I'm using the recommended approach and allocating the buffer in the VB.NET side, then passing this to the unmanaged dll.


The unmanaged functions are __stdcall and it's a Unicode build.




:confused:


-R.




What if instead of passing a StringBuilder object you pass a string and


Marshal it as UnmanagedType.BStr, does that work?




--


















"scot_paro" <scot_paro.3y5jco@invalid> wrote in message


news:scot_paro.3y5jco@invalid...

>

> Hi, I'm really struggling with this so any help would be appreciated.

> Outlook 2007, VS2008 Pro, VSTO 3, XP Pro x64 (dev) and x32 (test).

>

> I've a C++ dll with a bunch of functions to handle the registry, some

> custom authentication of the product, and some comms. I'd rather not

> have to rewrite them in VB.NET/C# (I'm primarily a C++ developer).

> There are extern "C" wrappers for the C++ calls.

>

> The product was originally done in VBA, and everything works fine there

> :)

>

> I've written an add-in and I've got the pinvoke stuff in place. I can

> call Win32 functions - for testing, I pop up a message box and get the

> system directory. All is well, works exactly as I'd expect.

> However, when I call the functions in my unmanaged dll, the functions

> get entered, but their arguments are garbage. I'm passing in a

> StringBuilder to get a string of text back, but writing to the

> StringBuilder parameter in the unmanged function (wchar_t*) causes an

> access violation.

> I've written a test project to make sure there's nothing funny with my

> machine setup. I've got my unmanaged dll and a VB.NET console app. The

> unmanaged functions return an int, accept a string, modify the string as

> a side effect, and it all works fine.

> But, when I do exactly the same thing in the add-in, it fails :(

>

> Is there something I'm missing? Is this a CAS issue? Does VSTO just

> not like unmanaged code? :confused:

>

> Thanks.

> -R.

>

>

> --

> scot_paro

>

>
 

scot_paro

Member
Fixed it!! :D




It's always the way of these things. As I was typing my first reply I was thinking about the first parameter I was passing and wondering if I was somehow corrupting the stack with it. Well, I was! I was passing the address as a Long, which in VB.NET land is 64 bits. My functions were declared as having 4 byte parameters in C/C++ land (32 bit), so when my 64 bit value landed in a space expecting a 32 bit value, things got a bit out of shape after that. D'Oh!


I've changed the Long to an Integer (I should probably really marshal it as a pointer) and everything is working fine.




-R.






Thanks Ken, but it makes no difference at all. I've changed my test program and my application, and the test program works fine (as it always has) but the VSTO application fails with an access violation, as before.




The really confusing thing is that the first argument in the unmanaged functions is the address of an object obtained via the dll - I'm wrapping a C++ object - and this works fine.


I'm using the recommended approach and allocating the buffer in the VB.NET side, then passing this to the unmanaged dll.


The unmanaged functions are __stdcall and it's a Unicode build.




:confused:


-R.
 
Status
Not open for further replies.
Thread starter Similar threads Forum Replies Date
T Way to avoid annoying typecasts? VSTO V3 Using Outlook 3
M Develop for OL2003 on OL2007 machine using VS2008 and VSTO? Outlook VBA and Custom Forms 1
A Not able to load an add-in for outlook 2007 developed in VSTO 2005 Outlook VBA and Custom Forms 2
E VSTO 2008 how to bind Form Region controls to Item data fields Outlook VBA and Custom Forms 2
E VSTO 2008 how to bind Form Region controls to Item data fields Outlook VBA and Custom Forms 3
T VSTO Outlook add-in: Categories not displaying correctly Outlook VBA and Custom Forms 3
O VSTO - Outlook Add-In - Get GAL data Outlook VBA and Custom Forms 1
M VSTO deployment? Outlook VBA and Custom Forms 1
K Outlook VSTO Add-in not visible in Trust Center after installing Outlook VBA and Custom Forms 1
M VSTO C#: How do I declare an application scope variable? Outlook VBA and Custom Forms 2
M Will a VSTO C# Outlook 2007 Add-in work on Outlook 2010? Outlook VBA and Custom Forms 1
X Highlight block of text in an outlook mail using c#2008 vsto Outlook VBA and Custom Forms 1
W My VSTO 3.0 Outlook addin doesn’t load Outlook VBA and Custom Forms 1
G Interop or VSTO or ? Outlook VBA and Custom Forms 2
M VSTO 2007 Addin auto-disabled on one machine but not another on installation? Outlook VBA and Custom Forms 1
N VSTO outlook 2007 addin installation problem Outlook VBA and Custom Forms 1
O Third-party VSTO 2005 SE plug-in for Outlook 2007: what should I signwith a publisher certificate? Outlook VBA and Custom Forms 3
O Outlook VSTO 2005 plug-in installer: CLSID = ? Outlook VBA and Custom Forms 3
O using VSTO to get Available Mailboxes Outlook VBA and Custom Forms 11
M Outlook2007 and VSTO, handle the Click on the Save Button in the IPM.Note dialog HOWTO? Outlook VBA and Custom Forms 4
M VSTO Outlook2007 Addin. After switching from 3.5 to 2.0 Framework i get "'MSB3185: EntryPoint not sp Outlook VBA and Custom Forms 1
E Opening WinForm in Outlook 2003 VSTO Addin with Wordmail enabled Outlook VBA and Custom Forms 1
C Problem installing VSTO AddIn on Vista Outlook VBA and Custom Forms 3
J Calling a Public sub-routine from the script editor via VB script Outlook VBA and Custom Forms 4
Wayne Dardis Calling Ribbon Button in Outlook 2016 Outlook VBA and Custom Forms 6
A Calling one module from another Outlook VBA and Custom Forms 2
I Calling from Outlook Using Outlook 2
G Latest Exchange Rollup - Management Console Error - Exception calling "GetSteppablePipeline" Exchange Server Administration 1
Diane Poremsky Re: Outlook 2010 - Calling does not work - 2nd request for help Using Outlook 0
R Outlook 2010-Calling Does not Work-3rd Request for Help Using Outlook 3
J Outlook 2010 - an error occurred while calling the callback: "Ribbon GetDCVisible" Using Outlook 4
H Auto-Dialer for calling contact Using Outlook 2
T PR_MESSAGE_CLASS is reset when calling Save() on item Outlook VBA and Custom Forms 4
Similar threads

































Top