473,324 Members | 2,356 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,324 software developers and data experts.

Outlook security, MAPI32 and RtlMoveMemory (oh my!)

Howdy, all.

I am currently looking for a way to, using VB.NET, get around those
annoying little "Allow Access for X Minutes" dialogs that pop up in
Outlook 2003, without resorting to 3rd party add-ins, utilities, or
changes to Exchange Server policies.

After a lot of digging around, I came across an article on
MAPILab.com that contained some VB6 source code. Here's the URL for
those who are curious:

http://www.mapilab.com/articles/vb_o...ecurity_4.html

Basically the article involves hooking into MAPI32.DLL via API
calls, which gives you a pointer to a structure (SPropValue), which in
turn contains a pointer (stored in val1) to a string variable which
contains the value I want. Anyway, I tried it in VB6, and it worked
like a charm. Exactly what I want. Here's the working VB6 source
code:

Private Declare Function HrGetOneProp Lib "mapi32" Alias
"HrGetOneProp@12" ( _
ByVal lpMapiProp As IUnknown, _
ByVal ulPropTag As Long, _
ByRef lppProp As Long) As Long

Private Declare Function MAPIFreeBuffer Lib "mapi32" ( _
ByVal lppProp As Long) As Long

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
Destination As Any, Source As Any, ByVal Length As Long)

Private Declare Function lstrlenA Lib "kernel32.dll" ( _
ByVal lpString As Long) As Long

Private Type SPropValue
ulPropTag As Long
dwAlignPad As Long
val1 As Long
val2 As Long
val3 As Long
End Type

Private Function LPSTRtoBSTR(ByVal lpsz As Long) As String
Dim cChars As Long
cChars = lstrlenA(lpsz)
LPSTRtoBSTR = String$(cChars, 0)
CopyMemory ByVal StrPtr(LPSTRtoBSTR), ByVal lpsz, cChars
LPSTRtoBSTR = Trim(StrConv(LPSTRtoBSTR, vbUnicode))
End Function

Public Sub PrintEmail()
Dim objSesson As Object
Dim objItem As Object

Set objSession = CreateObject("MAPI.Session")
objSession.Logon
Set objItem = objSession.Inbox.Messages.GetFirst

Dim ptrSProp As Long
ptrSProp = 0
If HrGetOneProp(objItem.MAPIOBJECT, CdoPR_SENDER_NAME, ptrSProp) =
0 Then
Dim sprop As SPropValue
CopyMemory sprop, ByVal ptrSProp, 20
MsgBox LPSTRtoBSTR(sprop.val1)
MAPIFreeBuffer ptrSProp
End If

Set objItem = Nothing
Set objSession = Nothing
End Sub

Now I am trying to convert it to VB.NET, and this is where it all
goes horribly pear-shaped. I ran it through the upgrader (just to see
what the heck I SHOULD be doing), took care of all the comments and
such, and I have read previous postings regarding upgraded API calls
and believe I have what SHOULD be working code.

However, when I run the new application, the resulting structure
does not contain any non-zero values, and the final string I want is
also blank. Could someone take a look at my source code (posted below)
and see if anything looks glaringly obvious? Or perhaps suggest a
different solution altogether? I'm guessing I'm just doing something
wrong with my CopyMemory calls, but God only knows...

If all else fails, I'll simply turn the VB6 code into a DLL and
just do the COM wrapper thing, but that seems really kludgy, so I'd
like to see this work in .NET.

Thanks in advance! Here's the new source code. Sorry about the
length:

Imports System.Runtime.InteropServices
Imports Microsoft.Office.Interop
Imports Microsoft.Office.Interop.Outlook
Imports MAPI

Module MapiProps
#Region " API Calls "
Public Declare Auto Function HrGetOneProp Lib "mapi32.dll" Alias
"HrGetOneProp@12" ( _
<MarshalAsAttribute(UnmanagedType.IUnknown)> ByVal lpMapiProp
As Object, _
ByVal ulPropTag As Integer, _
ByRef lppProp As Integer) As Integer

Private Declare Function MAPIFreeBuffer Lib "mapi32" (ByVal lppProp
As Integer) As Integer

Private Declare Sub CopyMemoryStruct Lib "kernel32" Alias
"RtlMoveMemory" ( _
ByVal Destination As SPropValue, _
ByVal Source As Integer, _
ByVal Length As Integer)

Private Declare Sub CopyMemoryString Lib "kernel32" Alias
"RtlMoveMemory" ( _
ByVal Destination As String, _
ByVal Source As Integer, _
ByVal Length As Integer)

Private Declare Function lstrlenA Lib "kernel32.dll" (ByVal
lpString As Integer) As Integer
#End Region

Private Structure SPropValue
Dim ulPropTag As Integer
Dim dwAlignPad As Integer
Dim val1 As Integer
Dim val2 As Integer
Dim val3 As Integer
End Structure

Private Function LPSTRtoBSTR(ByVal lpsz As Integer) As String
Dim cChars As Integer = lstrlenA(lpsz)
Dim strValue As String = New String("0", cChars)
CopyMemoryString(strValue, lpsz, cChars)
Return Trim(strValue)
End Function

Public Sub PrintEmail()
Dim objSession As Object
Dim objSesson As Object
Dim objItem As Object

objSession = CreateObject("MAPI.Session")
objSession.Logon()
objItem = objSession.Inbox.Messages.GetFirst

Dim ptrSProp As Integer
ptrSProp = 0
Dim sprop As SPropValue
If HrGetOneProp(objItem.MAPIOBJECT,
MAPI.CdoPropTags.CdoPR_SENDER_NAME, ptrSProp) = 0 Then
CopyMemoryStruct(sprop, ptrSProp, 20)
MsgBox(LPSTRtoBSTR(sprop.val1))
MAPIFreeBuffer(ptrSProp)
End If

Object_Dispose(objItem)
Object_Dispose(objSession)
End Sub

Public Sub Object_Dispose(ByVal aobjItem As Object)
' Add the following statement to the top of any vb file where
you place this Sub
' Imports System.Runtime.InteropServices
Dim count As Integer
Debug.WriteLine("DisposeObject Called")
Try
If aobjItem Is Nothing Then
Exit Try
End If
count = Marshal.ReleaseComObject(aobjItem)
Debug.WriteLine(String.Format("DisposeObject - Release {0},
RefCount: {1}", aobjItem.ToString(), count), "")
While count > 0
count = Marshal.ReleaseComObject(aobjItem)
End While
Catch ex As SystemException
Debug.WriteLine(String.Format("DisposeObject Exception:
{0}"), ex.Message)
Finally
aobjItem = Nothing
End Try
End Sub
End Module

Nov 21 '05 #1
0 2984

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

Similar topics

2
by: Kimmo Laine | last post by:
Hello, can i set MSMQ security options from my code: I want that whenever i create a new queue, certain rights ( open, read and sen ) are given to certain group. thx Kimmo Laine
0
by: Mads W. | last post by:
Hi, I'm coding a small program that can create new mails in outlook. So far I'm avoding the security warning by only displaying the created mail, and let the user interact to send the mail. But...
1
by: Rohith | last post by:
I have written a C# application (not a add in) to access outllook. I retrieve information from Contacts,Inbox and notes folder. But when i run the application I get a Security warning from outlook...
3
by: John | last post by:
Hi I am getting the notorious "an application is trying to access your application..." whenever I try to send an email using outlook from within a vb.net app. Is there a way to bypass this? I am...
14
by: John | last post by:
Hi Is there a way to e-mail using outlook2002 from within vb.net without getting the nasty "An application is trying to access you outlook..." message? Thanks Regards
3
by: Yavuz Bogazci | last post by:
Hi, i am connecting to my outlook via vb.net and mapi. When i want to access an Mailitem an get its properties i get an Security Message form Outlook popping up saying somethin like "A program...
16
by: ML | last post by:
Is there anyway to get around the security prompt from Outlook when using the object in a vb.net winforms app? I want to be able to load the contacts lists from Outlook but each time it access it...
3
by: =?Utf-8?B?UGF1bERIQw==?= | last post by:
Hi all, another outlook question I'm afraid. is there a way of disabling the security settings within outlook. When my app tries to send an email, a little message appears stating that another...
4
by: Mike P | last post by:
Is it possible to prevent Outlook security warnings without using a 3rd party tool (i.e. is there some code you can write yourself to do this)? *** Sent via Developersdex...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.