Tuesday
May292012
QTP - Calling C# DLL in VBScript
![Date Date](/universal/images/transparent.png)
A while ago I wanted to call a C# dll from a QTP script. I posted my question here on stackoverflow.
Short Explanation
- Create a C# dll using Visual Studio.
- Use the DotNetFactory in QTP to call the public methods in the C# dll.
Step 1 - Create a C# DLL
Create Visual Studio Solution and Project
- Start Visual Studio 2010
- File > New > Project
- .NET Framework 3.5
- Visual Studio > Windows > Class Library
- Name = mydll
- Location = c:\
- Solution name = ClassLibrary
- OK
![](/storage/blog-images/SMLLC_Blog_VisualStudio_1.png?__SQUARESPACE_CACHEVERSION=1338311706988)
Create Public Method
- Rename Class1.cs to functions.cs. Yes to rename all references.
- Create a public method: public int GetValue() { return 34; }
![](/storage/blog-images/SMLLC_Blog_VisualStudio_2.png?__SQUARESPACE_CACHEVERSION=1338311910646)
Build DLL
- Save
- Build > Build Solution
- Look in C:\ClassLibrary\mydll\bin\Debug (or Release) for the mydll.dll
- Close Visual Studio 2010
![](/storage/blog-images/SMLLC_Blog_WindowsExplorer_6.png?__SQUARESPACE_CACHEVERSION=1338311985465)
Step 2 - Create QTP Script
Create QTP Script
- Start QTP
- File > New > Test
- Set obj = DotNetFactory.CreateInstance("mydll.functions", "c:\\classlibrary\\mydll\\bin\\debug\\mydll.dll")
- MsgBox "GetValue() from C# dll returns : " & obj.GetValue()
![](/storage/blog-images/SMLLC_Blog_QTP_1.png?__SQUARESPACE_CACHEVERSION=1338312053400)
Run QTP Script
- Run the script.
- Message box with '34' is displayed.
![](/storage/blog-images/SMLLC_Blog_QTP_2.png?__SQUARESPACE_CACHEVERSION=1338312073169)
Reader Comments