Get Window Handle, Working With API

I'm new to working with an API, and I need to send it a handle to my calling application main window.

In my VB project, I'm trying to make a link to a C++ API via my Form, Form4. At current, I've placed a call to the function to make a link to the API in the Button.Click() sub for each button I click on that does something. I'm currently passing it a handle like this each time:

Me.Handle.ToInt32

What I'd like to do is establish the connection once by clicking on a certain button, and have the connection still work each time I click any other button on my form. At current, I've only been able to make it work by re-establishing the connection in each Button.Click() sub for each button I want to click on that will do different things with the API. I'm wondering how I would go about doing this once. Am I not passing the right handle to my form?
diablo089Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

lazyberezovskyCommented:
Use
[DllImport("USER32.DLL")]
private static extern IntPtr SetParent(IntPtr child, IntPtr parent);
0
diablo089Author Commented:
Hmm... is this the right thing to do if I just work from a single form? I don't bring up any other forms when I click the buttons to perform actions.
0
diablo089Author Commented:
What I'm doing is connecting to a database via an external API. If I establish the connection in the Button.Click() sub of each button on my form, it works fine. If I only put the API call to establish the connection in one button.click() sub, when I go to click any other button on my form, the connection is apparently dead, because my API calls don't work.

I just started using API calls recently, so I might not have the right methodology about this, or I might be doing something wrong.
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

CodeCruiserCommented:
Show us the code you are using.
0
diablo089Author Commented:
Prototypes of functions:

Declare Function API_Init Lib "API2.dll" (ByVal Handle As Integer, ByVal Path As String) As Integer
Declare Function API_Open Lib "API2.dll" (ByVal Path As String) As Integer

I use it in a Button.Click() sub like this:

Dim rInit As Integer
Dim rOpen As Integer
rInit = API_Init(Me.Handle, vbNullString)
rOpen = API_Open(Path)

So, if I put this code in each of my button.click subs for my various buttons, it works just fine. What I'd like to know is if I can put this in a single button.click sub, establishing the connection, and have the API connection still connected for any other button click.

At current, if I put it in a single button.click sub, my connection is dead in any other button.click sub that I don't put the connection code in. First I thought it might be some kind of scope issue, but I dunno.
0
diablo089Author Commented:
Any ideas?
0
CodeCruiserCommented:
Dont know what this API is so I think you need to reinitialize the API as this may be the limitation of the API.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.